The only way to do this is with multiple statements. Using dynamic sql, you can do this by separating each statement in the query string with a colon:
"DECLARE @ID int;INSERT INTO [Entry] (...) VALUES ...; SELECT @ID = scope_identity();INSERT INTO [TAGS] (ID_ENTRY) VALUES (@ID);"
Make sure you put this in a transaction to protect against concurrency problems and keep all atomic. You can also split this into two separate queries to return a new ID value in the middle if you want; just make sure both requests are in the same transaction.
Also: you use parameterized queries with your dynamic sql, right? If you do not, I personally will come there and strangle you 10,000 times with wet noodles until you repent of your insecure ways.
Joel Coehoorn
source share