在sql server中存在多个if if语句? [英] multiple if exists statements in sql server?

查看:845
本文介绍了在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 outer if 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆