在一个过程中,使用多个if else条件,并以begin结束 [英] in a procedure use multiple if else condition with begin end

查看:81
本文介绍了在一个过程中,使用多个if else条件,并以begin结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这样的条件下编写一个类似这样的proc程序,我想触发两个语句

i want to write a proc something like this on condition i want to fire two statements

create proc abc()
begin
if exists(query)
 begin
  update something // here two lines
  select something
 end 
 else if  not exists(query)
 begin
  insert something // here two lines
  select something
 end
 end
end

推荐答案

参考:
IF ... ELSE(Transact-SQL) [
Refer:
IF...ELSE (Transact-SQL)[^]

Try this:
IF EXISTS (Select * from bom_steel where fpart = @fpart)
        BEGIN
            INSERT INTO bom_steel
            VALUES (@fPart,@fL,@fH,@fW,@fDesc)
        SET END
    ELSE
        BEGIN
            UPDATE bom_steel
            SET fl=@fl, fh=@fH, fW=@fW
            where fpart=@fpart
        SET END


看看其他一些类似的讨论:
SQL存储过程-如果存在,则更新,否则插入. [ SQL Server:更新行(如果存在)的最佳方法,如果不行则插入 [ ^ ]


Look at some more similar discussions:
SQL Stored Procedure - if exists Update else Insert.[^]
SQL Server: Best way to Update row if exists, Insert if not[^]


文档 [
The Documentation[^] would seem an obvious place to start?

<br />
If exists(query) then<br />
    update something<br />
    select something<br />
    else if not exists(query) then<br />
        insert something<br />
        select something<br />
    end if<br />
end if<br />


这篇关于在一个过程中,使用多个if else条件,并以begin结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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