我目前正在为我的项目使用SQL Server 2012和Visual Studio 2012。
我的问题是我在下面有以下代码,它包含声明变量。基本上,当我在我的SQL服务器中使用它时,我将@StartDate设置为当月的第1天,将@EndDate设置为当月的最后一天(12月)。
DECLARE @StartDate date
DECLARE @EndDate date
-- SET @StartDate = '20161201'
-- SET @EndDate = '20161231'
SELECT A.[TYPE_NAME], -- Type_Name
A.[STRT_DTTM], --Start Date
A.[END_DTTM], --End Date
B.[ASSIGNED_DT], --Assigned Date
CASE WHEN (CAST(A.[STRT_DTTM] AS DATE) <= @EndDate AND (CAST(A.[END_DTTM] as date) > @EndDate OR A.[END_DTTM] IS NULL))
THEN 1
ELSE 0
END AS 'OpenIndicator',
CASE WHEN CAST(A.[END_DTTM] as DATE) between @StartDate AND @EndDate
THEN 1
ELSE 0
END AS 'ClosedIndicator',
CASE WHEN CAST(B.[ASSIGNED_DT] as DATE) between @StartDate AND @EndDate
THEN 1
ELSE 0
END AS 'AssignedIndicator',
FROM Report AS A
INNER JOIN Dimension AS B
ON A.[ID] = B.[ID]
WHERE 1=1
AND A.[EXPIRE_DATE] = '12/31/9999' --Expire Date (Basically looking for active records.)
AND (A.[TYPE_NAME] like '%Bicycle%'
OR A.[TYPE_NAME] like '%Car%')
到目前为止,在SSRS中,我已将上述代码放入数据集中。我还在SSRS StartDate和EndDate中创建了2个参数。当我尝试根据数据集运行一个简单的Tablix时......没有任何内容出现。
我注释掉了SET @StartDate ='20161201'和SET @EndDate ='20161231'。
我对工作中的工作非常有限,所以我不能不幸地使用存储过程,因为这可以让我更容易思考。
但是我试图通过用户输入两个参数StartDate和EndDate来填充Sales之类的数据集,但我在SSRS中遇到了麻烦。任何人都可以帮我准确地做什么,我很失落。
试图在代码中发表评论,以便更容易理解并且由于不在工作中遇到麻烦而不得不改变一些事情。
答案 0 :(得分:1)
您不需要在查询中为数据集声明变量,这在SSRS应用程序层中处理。您的查询应该是......
SELECT A.[TYPE_NAME], -- Type_Name
A.[STRT_DTTM], -- Start Date
A.[END_DTTM], -- End Date
B.[ASSIGNED_DT], -- Assigned Date
CASE WHEN (CAST(A.[STRT_DTTM] AS DATE) <= @EndDate
AND (CAST(A.[END_DTTM] as date) > @EndDate OR A.[END_DTTM] IS NULL))
THEN 1
ELSE 0
END AS 'OpenIndicator',
CASE WHEN CAST(A.[END_DTTM] as DATE) between @StartDate AND @EndDate
THEN 1
ELSE 0
END AS 'ClosedIndicator',
CASE WHEN CAST(B.[ASSIGNED_DT] as DATE) between @StartDate AND @EndDate
THEN 1
ELSE 0
END AS 'AssignedIndicator',
FROM Report AS A
INNER JOIN Dimension AS B
ON A.[ID] = B.[ID]
WHERE 1=1
AND A.[EXPIRE_DATE] = '12/31/9999'
AND (A.[TYPE_NAME] like '%Bicycle%'
OR A.[TYPE_NAME] like '%Car%')
由于您已经提到您已在SSRS中创建参数,请右键单击DataSet
并转到Properties
,然后在Parameters
标签上,映射您的SSRS参数到你的查询参数。
最后,如果您希望默认日期为当月的1天和最后一天,您可以转到Parameters
,右键点击转到Properties
,转到Default Values
选项卡并单击Add
并使用以下表达式。
当月的第一天
=Today.AddDays(1-Today.Day)
当月的最后一天
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)