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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 is there a combined update/insert command?

Author  Topic 

gdeconto
Posting Yak Master

107 Posts

Posted - 2002-06-12 : 17:36:48
Its been a long day so apologies if this sounds stupid but...

I find myself doing a lot of stored procedures where I have to test for the existance of a record before I can decide whether to use INSERT or UPDATE. For example

IF EXISTS(SELECT foo FROM mytable WHERE mycondition=true)
UPDATE mytable SET myfields=myvalues
ELSE
INSERT mytable (myfields) VALUES (myvalues)

Is there a flag or command or something that will allow me to bypass the need for testing, and have sql update the record if it exists and insert if it doesn't?? For example:

JUSTDOIT mytable (myfields) VALUES (myvalues)


robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-06-12 : 17:48:50
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=13036



Anyway, what's the big deal? You can't combine the two operations because they are totally separate in function (INSERT and UPDATE) If they weren't separate operations you wouldn't need two commands for them!

Is it performance problems or just coding complexity that's bothering you?

Hey, you should be proud of yourself, you're taking proactive measures to ensure data integrity instead of relying on error messages like 99% of programmers do, who then complain that SQL Server won't let them do an illegal operation!

Go to Top of Page

gdeconto
Posting Yak Master

107 Posts

Posted - 2002-06-12 : 18:25:05
Thanks for the info, Rob.

There's an old saying that talks about how all the great inventions in the world were invented by basically lazy people who just wanted to save themselves some time/effort.

I guess that that makes me a very inventive/great person as I like to keep my coding to an absolute minimum.

The link you provided had a couple of very interesting suggestions for Microsoft. I guess I was looking for the UPSERT or INDATE functionality.

cheers



Go to Top of Page
   

- Advertisement -