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
 SQL Server Development (2000)
 What is more efficient

Author  Topic 

jonasnas
Starting Member

1 Post

Posted - 2011-02-04 : 06:19:26
Hi,
I'm writing a java application and I need to update or insert a row if it doesn't exist already. So I wonder what is the best approach for this. Now I just delete the row (if the data existed it is deleted if not than nothing happens) and insert the new one, but may be its more efficient if i make a select at first, then if smth is returned, I will update and if not I will insert. Or I should stay with current implementation?

Thanks

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-02-04 : 06:34:13
UPDATE first. If @@ROWCOUNT returns zero records you will have to INSERT instead.

UPDATE dbo.Table1
SET Col1 = '...'
WHERE pkCol = @Value

IF @@ROWCOUNT = 0
INSERT dbo.Table (Col1) VALUES('...')



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -