简体   繁体   English

如何更改为SSRS报告执行的SQL

[英]How do I change the SQL executed for an SSRS report

I'm using SQL Server 2008R2 with SSRS 2010. I'm testing a stored procedure that uses an if statement to switch between execution of one of 2 simple SQL statements. 我将SQL Server 2008R2与SSRS 2010一起使用。我正在测试使用if语句在两个简单SQL语句之一的执行之间进行切换的存储过程。 My ultimate goal is to replace SQL embedded in several SSRS reports with stored procedures that choose which statement to run based on a date parameter. 我的最终目标是用存储过程替换一些SSRS报告中嵌入的SQL,该存储过程根据日期参数选择要运行的语句。

I tested the statement in SSMS and it switches between statements and produces results with no problem. 我在SSMS中测试了该语句,它可以在语句之间切换,并且可以毫无问题地产生结果。 However, in SSRS when I execute the report and test for the 2nd statement, except for a computed text column my result set is empty. 但是,在SSRS中,当我执行报告并测试第二条语句时,除了计算的文本列外,我的结果集为空。 If I run the same proc from the SSRS query designer the switch between statements executed works 如果我从SSRS查询设计器运行相同的proc,则执行的语句之间的切换有效

Can anyone help me understand why the switch does not work when executing through the report? 谁能帮助我了解为什么在执行报告时该开关不起作用? Here's the SQL I'm using: 这是我正在使用的SQL:

begin
if @reportdate >='10/1/2014' goto convformat
if @reportdate <'10/1/2014' goto preconvformat
end

preconvformat:

SELECT TOP (1000) col1
    ,col2
    ,col3
    ,col4
    ,col5
    ,col6
    ,col7
    ,'t1' AS OriginalDataSource
FROM t1 AS arh WITH (NOLOCK)
LEFT JOIN t1a1 a WITH (NOLOCK)
    ON a.col1 = arh.col1
WHERE (arh.DateCreated < '1/1/2015')
and a.col2 = '12345678'

goto testend    

convformat:

SELECT TOP 1000 arcus.col1
    ,col2
    ,col3
    ,col4
    ,col5
    ,col6
    ,col7
    ,'t3' AS OriginalDataSource
FROM t3. rmrtrk WITH (NOLOCK)
LEFT JOIN t3t1 arcus WITH (NOLOCK)
    ON arcus.col1 = rmrtrk.col1

where customer_number = '12345678'

testend:

Create a procedure and use ANSI Date format to compare the passed date values .. 创建一个过程并使用ANSI日期格式比较通过的日期值。

Also avoid using GOTO , very difficult to debug in larger pieces of code. 还避免使用GOTO ,这在较大的代码段中很难调试。 Use IF..ELSE constructs. 使用IF..ELSE构造。

CREATE PROCEDURE Test_Proc 
 @reportdate  DATE 
AS
BEGIN
  SET NOCOUNT ON;

  IF (@reportdate >= '20141001')  --<-- ANSI Date Format  YYYYMMDD
   BEGIN
     -- 1st Statement 
   END
  ELSE
   BEGIN
    -- 2nd Statement 
   END

END

I just realized what the problem is. 我才意识到问题出在哪里。 Initially, I was going to use a union statement, where the 2nd statement was filtered and merged with the first. 最初,我将使用union语句,其中第二条语句已过滤并与第一条合并。

When I change my approach, I forgot to use aliases for the 2nd statement. 更改方法时,我忘记为第二条语句使用别名。 This is why the SSRS result set was empty. 这就是SSRS结果集为空的原因。

M. Ali, thank you for your contribution. 阿里先生,谢谢您的贡献。 I'll make appropriate changes based on your feedback. 我会根据您的反馈进行适当的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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