Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
How can I know that the INSERT ran so as not to do the update?
INSERT INTO Objects (AccountID)OUTPUT INSERTED.ID INTO Othertable (ID) SELECT AccountID FROM OPENXML (@hdoc, '/Objects/Record', 1) WITH (AccountID int) xmlElements WHERE NOT EXISTS (SELECT ID FROM Objects WHERE Objects.AccountID = xmlElements.AccountID)--ELSE DO AN UPDATE UPDATE Objects SET Objects.....
I was trying to do the following. But @@IDENTITY returns the identity of OUTPUT INSERTED.ID INTO Othertable (ID)(correctly) even though it's not doing an insert (incorrectly???):
DECLARE @Inserted intSET @Inserted = 0 INSERT INTO Objects (AccountID)OUTPUT INSERTED.ID INTO Othertable (ID) SELECT AccountID FROM OPENXML (@hdoc, '/Objects/Record', 1) WITH (AccountID int) xmlElements WHERE NOT EXISTS (SELECT ID FROM Objects WHERE Objects.AccountID = xmlElements.AccountID)SELECT @Inserted = @@IDENTITY IF @Inserted = 0begin UPDATE Objects SET Objects.....end
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-01-27 : 09:09:13
what about this?
DECLARE @Inserted intSET @Inserted = 0 INSERT INTO Objects (AccountID)OUTPUT INSERTED.ID INTO Othertable (ID) SELECT AccountID FROM OPENXML (@hdoc, '/Objects/Record', 1) WITH (AccountID int) xmlElements WHERE NOT EXISTS (SELECT ID FROM Objects WHERE Objects.AccountID = xmlElements.AccountID)SELECT @Inserted = @@ROWCOUNT IF @Inserted = 0begin UPDATE Objects SET Objects.....end
John Sourcer
Yak Posting Veteran
91 Posts
Posted - 2009-01-28 : 09:11:33
Visakh16,Thanks but doesn't that give me the last iterations @@ROWCOUNT i.e. It gets all the rows from OPENXML processing them all and then moves onto the update?
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-01-28 : 09:23:44
what do you mean by last iteration? you're not looping but just inserting everything by single insert.