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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert/update in one stored procedure
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tpiazza55
Posting Yak Master

162 Posts

Posted - 07/30/2007 :  10:33:16  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2875 Posts

Posted - 07/30/2007 :  10:39:48  Show Profile  Reply with Quote
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

Sweden
30421 Posts

Posted - 07/30/2007 :  10:40:32  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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

Sweden
30421 Posts

Posted - 07/30/2007 :  10:41:02  Show Profile  Visit SwePeso's Homepage  Reply with Quote




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

tpiazza55
Posting Yak Master

162 Posts

Posted - 07/30/2007 :  10:52:06  Show Profile  Reply with Quote
fast answer thanks
Go to Top of Page

Kristen
Test

United Kingdom
22858 Posts

Posted - 07/30/2007 :  11:04:21  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000