如何将整个SQL查询放在CASE语句中

时间:2016-08-16 15:18:31

标签: sql-server-2012

在我运行它而不使用switch case的情况下运行的情况下运行正常的查询运行正常但是当我把这个查询放在case中它开始给出错误时如何更改我的查询以在switch case中运行

Declare @number int 
set @number = 1


SELECT
    CASE @number
       WHEN 1 THEN (SELECT     
                        userId, checkfreeId, role, buname, fiservEmailId 
                    FROM      
                        (SELECT     
                             tbl_buLogin.userId, tbl_buLogin.checkfreeId,
                             tbl_buroles.role, tbl_bu.buname, 
                             tbl_buLogin.fiservEmailId
                         FROM          
                             tbl_buLogin 
                         INNER JOIN
                             tbl_buroles ON tbl_buLogin.roleId = tbl_buroles.id 
                         INNER JOIN
                             tbl_userbujoin ON tbl_userbujoin.userId = tbl_buLogin.userId 
                         INNER JOIN
                             tbl_bu ON tbl_bu.buid = tbl_userbujoin.buId 
                                    AND tbl_bu.buid IN (SELECT tbl_userbujoin.buId 
                                                        FROM tbl_buLogin
                                                        INNER JOIN tbl_userbujoin ON tbl_buLogin.userId = tbl_userbujoin.userId
                                                        WHERE tbl_buLogin.checkfreeId = 'testiso')
                         UNION
                         SELECT     
                             tbl_buLogin_2.userId, tbl_buLogin_2.checkfreeId, tbl_buroles_1.role, tbl_bu_1.buname, tbl_buLogin_2.fiservEmailId
                         FROM         
                             tbl_buLogin AS tbl_buLogin_2 
                         INNER JOIN
                             tbl_buroles AS tbl_buroles_1 ON tbl_buLogin_2.roleId = tbl_buroles_1.id 
                         INNER JOIN
                             tbl_bu AS tbl_bu_1 ON tbl_bu_1.buid = tbl_buLogin_2.buId 
                                                AND tbl_bu_1.buid IN (SELECT tbl_userbujoin.buId 
                                                                      FROM tbl_buLogin
                                                                      INNER JOIN 
                                                                          tbl_userbujoin on tbl_buLogin.userId = tbl_userbujoin.userId
                                                                      WHERE tbl_buLogin.checkfreeId = 'testiso')

                         WHERE     
                             (tbl_buLogin_2.userId NOT IN (SELECT     tbl_userbujoin_1.userId
                                                           FROM tbl_buLogin AS tbl_buLogin_1
                                                           INNER JOIN tbl_userbujoin AS tbl_userbujoin_1 ON tbl_userbujoin_1.userId = tbl_buLogin_1.userId))) AS tbl 
                ORDER BY role)
        ELSE 'NO'
    END

1 个答案:

答案 0 :(得分:2)

为什么不使用IF-ELSE

IF @number = 1 THEN
BEGIN 
  --your Query here
END
ELSE
  SELECT 'NO'