在sql server中存在多个if if语句? [英] multiple if exists statements in sql server?
本文介绍了在sql server中存在多个if if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果 存在(选择 * 来自 K_RT_AmountReceived 其中 trno = @ trno)
如果 存在(选择 *来自K_RT_AmountReceived 其中 trno = @ trno 和 recamount = @ recamount )
开始
选择 ' 2' as status
end
else
开始
插入 into K_RT_AmountReceived(partyname, date ,recamount,chequeno,trno,updatedby,updatedon) values ( @ partyname , @ date , @ recamount , @ chequeno , @ trno , @ username ,getdate())
选择 ' 1' as status
end
i希望存在多个if语句..上面的代码显示错误
解决方案
你不需要:你的嵌套如果
包含外部中的子句,如果
还有!
所以只是抛弃外部条件......
试试这个
如果 存在(选择 * 来自 K_RT_AmountReceived 其中 trno = @ trno)
开始
如果 存在(从K_RT_AmountReceived 中选择 *其中 trno = @ trno 和 recamount = @ recamount )
开始
< span class =code-keyword> select ' 2' as 状态
< span class =code-keyword> end
else
begin
插入 进入 K_RT_AmountReceived(partyname, [ date ] ,recamount,chequeno,trno,updatedby,updatedon) values ( @partyname , @ date , @ recamount , @chequeno , @ trno , @ username ,getdate())
选择 ' 1' as status
end
end
如果 存在(从中选择 * K_RT_AmountReceived 其中 trno = @ trno)
和 存在(选择 *来自K_RT_AmountReceived 其中 trno = @ trno 和 recamount = @ recamount )
开始
选择 ' 2' as status
end
else
开始
i插入 进入 K_RT_AmountReceived(partyname, date ,recamount,chequeno,trno,updatedby,updatedon) 值( @ partyname , @ date , @ recamount , @ chequeno , @ trno , @ username ,getdate())
选择 ' 1' as status
端跨度>
if exists(select * from K_RT_AmountReceived where trno=@trno)
if exists(select *from K_RT_AmountReceived where trno = @trno and recamount = @recamount)
begin
select '2' as status
end
else
begin
insert into K_RT_AmountReceived(partyname,date,recamount,chequeno,trno,updatedby,updatedon)values(@partyname,@date,@recamount,@chequeno,@trno,@username,getdate())
select '1' as status
end
i want to exists multiple if statements..above code shows an error
解决方案
You don't need to: your "nested"if
contains the clause in the outerif
as well!
So just dump the outer condition...
try this
if exists (select * from K_RT_AmountReceived where trno=@trno) begin if exists(select *from K_RT_AmountReceived where trno = @trno and recamount = @recamount) begin select '2' as status end else begin insert into K_RT_AmountReceived(partyname,[date],recamount,chequeno,trno,updatedby,updatedon)values(@partyname,@date,@recamount,@chequeno,@trno,@username,getdate()) select '1' as status end end
if exists(select * from K_RT_AmountReceived where trno=@trno) and exists(select *from K_RT_AmountReceived where trno = @trno and recamount = @recamount) begin select '2' as status end else begin insert into K_RT_AmountReceived(partyname,date,recamount,chequeno,trno,updatedby,updatedon)values(@partyname,@date,@recamount,@chequeno,@trno,@username,getdate()) select '1' as status end
这篇关于在sql server中存在多个if if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文