开发者

SQL CASE Question

I don't know if this can be done but I thought I'd ask.

What I want to do is have a case statement query and if a 1 begin another action. if 0 do开发者_StackOverflow社区n't do anything.

For Example

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

Can this be done?


Not inside a CASE statement.

If you want to do control flow operations like this, use IF instead. The @dateofDeath variable can either be a T-sql variable you declare and assign, or it can be a SELECT statement itself.

IF @dateofDeath = 'yes' BEGIN
   do something
END ELSE BEGIN
   do something else
END


Do you mean this: do something if something is true/exists in another table?

IF EXISTS (SELECT * FROM Client WHERE deathofdeath = yes)
   SELECT stuff FROM OtherTable


Like unclepaul84 said you could do something like this:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜