SQL - 声明标量变量错误(用于SSRS报告)

时间:2015-02-25 13:26:43

标签: sql visual-studio-2010 variables reporting-services syntax

当我运行下面显示的查询时,这是我得到的错误:必须声明标量变量“@ReportDate”。 'Label'附近的语法不正确。 'GroupOrder'附近的语法不正确。必须声明标量变量“@ Demographics1”。

如何编写此查询以运行无错误?

我的ReportDate是日期时间 我的Demographics1是varchar(max)

以下是查询:

SELECT DISTINCT 
   'Age' DemographicGroup, CASE WHEN DATEDIFF(YY, [Date of Birth],  @ReportDate) < 25 THEN '<=25' 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) BETWEEN 25 AND 35 THEN '26-35' 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) BETWEEN 36 AND 45 THEN '36-45' 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) > 45 THEN '46+' 
                            END Label, 
                           CASE WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) < 25 THEN 1 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) BETWEEN 25 AND 35 THEN 2 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) BETWEEN 36 AND 45 THEN 3 
                                WHEN DATEDIFF(YY, [Date of Birth], @ReportDate) > 45 THEN 4 
                            END GroupOrder
INTO        #Demo2
FROM     [dbo].[Dim_Borrower]
UNION
SELECT DISTINCT 'Marital Status', [Marital Status], 1
FROM     [dbo].[Dim_Borrower]
UNION
SELECT DISTINCT 'Race', Race, 1
FROM     [dbo].[Dim_Borrower]
UNION
SELECT DISTINCT 'Sex', Sex, 1
FROM     [dbo].[Dim_Borrower]

                      SELECT   *,DemographicGroup + ' - ' + Label RealLabel
                      FROM     #Demo2
                      WHERE  DemographicGroup IN (@Demographics1)
                      ORDER BY DemographicGroup, GroupOrder

1 个答案:

答案 0 :(得分:1)

此时您已声明了这两个变量@ReportDate和@ Demographics1。

你必须有一些像

这样的行
Declare @ReportDate DateTime;
Delcare @Demographics1 varchar(max);