1
查詢寫入case內運行正常,當我運行它沒有使用開關的情況下,但是當我把這個查詢的情況下,它開始給錯誤我怎麼能改變我的查詢裏面的開關情況下運行如何把這個整個SQL查詢內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
非常亂碼後,我建議IF/ELSE IF - CASE不是流動的控制。 –
T-SQL中的CASE是*表達式*(如'a + b') - ** NOT **語句! 'CASE'最終將返回**一個單一的原子值** - 它是** NOT **旨在交替執行一組代碼或另一組代碼 –