我不知道這是否可以完成,但我想我會問。SQL CASE問題
我想要做的是有一個case語句查詢,如果1開始另一個動作。如果0不做任何事情。
對於實例
select CASE WHEN client.deathofdeath = yes THEN 1
do another select in here (which is another table)
Else 0 End AS DeathDate
From Client client
可以這樣做?
我不知道這是否可以完成,但我想我會問。SQL CASE問題
我想要做的是有一個case語句查詢,如果1開始另一個動作。如果0不做任何事情。
對於實例
select CASE WHEN client.deathofdeath = yes THEN 1
do another select in here (which is another table)
Else 0 End AS DeathDate
From Client client
可以這樣做?
不在CASE語句內。
如果你想做這樣的控制流操作,請使用IF。 @dateofDeath變量可以是您聲明和分配的T-sql變量,也可以是SELECT語句本身。
IF @dateofDeath = 'yes' BEGIN
do something
END ELSE BEGIN
do something else
END
你的意思是說:如果某件事情是真的/在另一個表中存在,做些什麼?
IF EXISTS (SELECT * FROM Client WHERE deathofdeath = yes)
SELECT stuff FROM OtherTable
就像unclepaul84說你可以做這樣的事情:
select
CASE
WHEN client.deathofdeath = yes THEN (select top 1 therow from othertable where clientid=clientid.clientid)
Else 0
End AS DeathDate
From
Client client
我不能確定你正試圖在這裏做什麼?另一個select是否打算返回僅適用於client.deathofdeath = yes或第二個結果集的附加列? – 2010-03-28 11:24:22
僅當「在這裏做另一個選擇」時纔會返回標量值(SELECTTEMT(*)來自AnotherTable) – unclepaul84 2010-03-29 19:05:30