简体   繁体   中英

SSRS Parameters Data Parameters Failing on Data Driven Subscription

I'm wondering if anyone is able to help.

I have a suite of data driven subscriptions which uses the following SQL Code to calculate the parameter. This is the same SQL Server data source that the report uses too.

 select cast(dateadd(day,case when datepart(dw,getdate()) in (5,6) then 4 else 2 end,getdate()) as date) [parameter]

The parameter in the report is of type date.

This has been working for 15 months no problem but following a server upgrade last week, this is now stopped working. When investigating, the error log said that the above was not giving a valid date. If I manually enter a date in the report itself it works fine. If I specify a hard coded date in the subscription, and look at the report, the results now appear to be reversing the month and day parts of the date (for when that option gives a valid date).

As a temporary work around in the critical reports, I've managed to get this working by converting the result to varchar in the format 'yyyy-MM-dd' by changing the code to this:

select left(convert(varchar,dateadd(day,case when datepart(dw,getdate()) in (5,6) then 4 else 2 end,getdate()),126),10) [parameter]

Because of the volume of reports affected, it's not necessarily practical to do this for all the reports.

Any ideas for causes and potential fixes?

I can see a reason to your code generates an error. You can get wrong results because of the configuration of SET DATEFIRST on the server.

First, check your Datadirst with

SELECT @@DATEFIRST  

And set it in your code like it

DECLARE @Date date = '20170914' -- Thursday


SET DATEFIRST 1 ; 
SELECT datepart(dw,@Date) 
--Result 4 

SET DATEFIRST 2 ; 
SELECT datepart(dw,@Date) 
--Result 3

SET DATEFIRST 3 ; 
SELECT datepart(dw,@Date) 
--Result 2

SET DATEFIRST 4 ; 
SELECT datepart(dw,@Date) 
--Result 1

SET DATEFIRST 5 ; 
SELECT datepart(dw,@Date) 
--Result 7

SET DATEFIRST 6 ; 
SELECT datepart(dw,@Date) 
--Result 6

SET DATEFIRST 7 ; 
SELECT datepart(dw,@Date) 
--Result 5

SET DATEFIRST (Transact-SQL)

SET DATEFORMAT

SET LANGUAGE (Transact-SQL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM