Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to write a proc something like this on condition i want to fire two statements
SQL
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
Posted
Updated 8-Jul-12 20:44pm
v2

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

Try this:
SQL
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

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[^]
 
Share this answer
 
Comments
barneyman 9-Jul-12 2:48am    
they quoted MySQL, which is subtly different to transact :)

[edit - removed gender bias]
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 />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900