Friday, February 10, 2012

Calling update and insert in one stored procedure

Hello,

Basically i want to have a stored procedute which will do an insert statement if the record is not in the table and update if it exists.

The Id is not autonumber, so if the record doesn't exists the sp should return the last id+1 to use it for the new record.

Is there any example i can see, or somebody can help me with that?

Thanks a lot.

IF EXISTS (SELECT * FROM table WHERE <condition>)

BEGIN

--the record exists. So just do the UPDATE

END

ELSE

BEGIN

--The record does not exist. So do an INSERT

END

|||

This is very helpful, thanks!

If it exists, then how can i get the max value of the id column?

Thanks again.

|||SELECT MAX(column) FROM table WHERE <Condition>|||

Thank you very much for your help!

No comments:

Post a Comment