Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert/update in one stored procedure

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2007-07-30 : 10:33:16
Hi:

I am trying to write a stored procedure that when passed a name, value it will

1. update the record if the name exists
2. create a new record if it doesnt

how would i do this?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2007-07-30 : 10:39:48
IF exists(select * from table1 where [name] = @name)
BEGIN
"UPDATE CODE HERE"
END
ELSE
BEGIN
"INSERT CODE HERE"
END

Jim
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-30 : 10:40:32
This
UPDATE ....

IF @@ROWCOUNT = 0
INSERT ...


or this
if exists (select * from table1 where name = @param)
update
else
insert




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-30 : 10:41:02




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2007-07-30 : 10:52:06
fast answer thanks
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-07-30 : 11:04:21
See also:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56148 Update / Insert Stored Procs
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=72835 Upsert proc, locking hints

Kristen
Go to Top of Page
   

- Advertisement -