Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi,I want to use a case statement with an if exists condition.Please look at the code below and let me know the issue please...Update temp_table Set State=@State, StateChanged=@DateChanged , LastEventUid= Case When If Exists (Select * From EventHeader Where Uid=@LastEventUid) then @LastEventUid else Null Where Uid=@AssetUid The issue is with the Case statement.. i get following errors:Incorrect syntax near the keyword 'If'.Incorrect syntax near the keyword 'then'.Thanks,Sourav
RickD
Slow But Sure Yak Herding Master
3608 Posts
Posted - 2009-07-15 : 06:46:25
What you can do is create a variable to hold a value and use isnull():
declare @exists intset @exists = (Select Uid From EventHeader Where Uid=@LastEventUid)Update temp_tableSet State=@State, StateChanged=@DateChanged , LastEventUid= Case When isnull(@exists,0) <> 0 then @LastEventUid else NullWhere Uid=@AssetUid
russell
Pyro-ma-ni-yak
5072 Posts
Posted - 2009-07-15 : 07:07:43
problem with this line:
LastEventUid= Case When If Exists (Select * From EventHeader Where Uid=@LastEventUid) then @LastEventUid else Null
should be
LastEventUid= Case When If Exists (Select * From EventHeaderWhere Uid=@LastEventUid) then @LastEventUid else Null END
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-07-15 : 13:16:10
also
Update tSet t.State=@State, t.StateChanged=@DateChanged , t.LastEventUid= Case When t1.cnt>0 then @LastEventUid else Null endfrom temp_table touter apply (Select count(*) as cnt From EventHeader Where Uid=@LastEventUid)t1Where t.Uid=@AssetUid