0

我正在为 am SSRS 报告创建一个存储过程。几乎,SP 从用户那里获取值,然后将其提供给 CTE 表。这是我的 SP 的样子:

@parameter1 varchar(12),
@parameter2 varchar(13),
@parameter3 varchar(13),
@parameter4 varchar(13)

;with cte1
     as (select column1, column2 from table1 
                 where column1 = @parameter1 and column2 = parameter2),
      cte2
     as (select column1, column2 from table3
                where column2 in (select column2 from cte1)
                AND column1 = @paramter3),
      cte3
     as (select column1, column2 from table4
               where column2 in (select column2 from cte2)
               AND column1 = @parameter4)

如果我使用 SELECT 语句,我可以在我的查询中加入这些 CTE。但是,要求发生了变化,现在我需要添加 IF 逻辑来说明“如果学校名称是 = @parameter1 然后执行此选择语句,否则执行此其他选择语句等等”。

这就是我为满足该要求所做的事情:

declare @1temp varchar(50)  --declaring this all the way at the top with other parameters

set @1temp = @parameter1

   If @1temp = 'Peabody'
      BEGIN 
           (Select from table1 where column1 = '1234')
      END

使用此逻辑时出现以下错误:
关键字“SET”附近的语法不正确。

我什至尝试了以下逻辑,但仍然没有成功:

    select @ltemp = column1 FROM cte3  --i made sure that only one value is being generated by that select statement
      IF @temp = 'Peabody'
         BEGIN

    (Select from table1 where column1 = '1234')
         END

我收到无效的对象名称“cte2”错误。

最后,我尝试了以下逻辑:

     set @1temp = (select column1 from cte3)   --i made sure that only one value is being generated by the select statement
          IF @1temp = 'Peabody'
               BEGIN

              (Select from table1 where column1 = '1234')
               END

有人可以告诉我我做错了什么吗?!

4

0 回答 0