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
 delete/insert vs update

Author  Topic 

tpiazza55
Posting Yak Master

162 Posts

Posted - 2008-01-22 : 09:42:09
Im trying to keep a mirror image of some data Im getting from Quickbooks.

As the records are inserted into the database I need to check if a record exists and either update or insert a new one.

it seems easier just to delete using the tnxid and reinsert vs updating

my question is if I go

begin

INSERT INTO QBInvoicesQue(100s of feilds)

end


begin

delete from QBInvoices where txnid = @TxnID

end

and there is not matching txnid to delete from will it cause any problems? before going to the insert statement?


begin

INSERT INTO QBInvoices(100s of feilds)

end




SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-22 : 09:44:30
IF EXISTS(SELECT * FROM QBInvoices WHERE txnid = @TxnID)
UPDATE ...
ELSE
INSERT ...



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

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-01-22 : 09:47:25
http://weblogs.sqlteam.com/mladenp/archive/2007/07/30/60273.aspx
http://www.sqlteam.com/article/application-locks-or-mutexes-in-sql-server-2005

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

tpiazza55
Posting Yak Master

162 Posts

Posted - 2008-01-22 : 09:54:29
is there any disadvantage to

IF EXISTS(SELECT * FROM QBInvoices WHERE txnid = @TxnID)

begin

delete from QBInvoices where txnid = @TxnID

INSERT INTO QBInvoices(stuff)

end

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-01-22 : 09:58:43
why would you want to do that when you can update?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-22 : 10:00:00
As Spirit is pointing out, there could potentially be a problem with [dead]locks.


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

tpiazza55
Posting Yak Master

162 Posts

Posted - 2008-01-22 : 10:14:12
its literally a hundred field -- guess i need to update to avoid the dead lock
Go to Top of Page
   

- Advertisement -