I call several sql files using:
:r C:\Scripts\Script1.sql
:r C:\Scripts\Script2.sql
:r C:\Scripts\Script3.sql
I have this idea about the :r
call from here: TransactSQL to run another TransactSQL script. The Script1.sql
script might as well also have other sql script calls in its code, etc.
But now I want to define settings for each script. For example: I define LastInsertedID
and set it to the value of SCOPE_IDENTITY()
right before the the call for Script1.sql
. And this script now uses this variable and works with it.
To do so I used sqlcmd script variables (http://msdn.microsoft.com/de-de/library/ms188714.aspx) and was setting them by using:
:setvar LastInsertedID SCOPE_IDENTITY()
I could then write SELECT $(LastInsertedID
in the Script1.sql
and it would give me the correct value.
Later I found out that this is not correct at all because if you process other INSERT
statements before you select $(LastInsertedID)
it would give you the ID of the newly inserted row. This might be the case because :setvar
does not save the value of that current SCOPE_IDENTITY()
but furthermore a reference to it and calls it again once it is requested.
So I tried something different and declared a variable, assigned it the current value of SCOPE_IDENTITY()
and then saved it with :setvar
. It looked something like this:
DECLARE @LastInsertedID int
SELECT @LastInsertedID = SCOPE_IDENTITY()
:setvar LastInsertedID @LastInsertedID
PRINT $(LastInsertedID)
This again worked for the moment and delivered the correct result. But then I separated my code into multiple GO
sections and realized that the :setvar
again delivers not the desired output.
When you insert a GO
after the :setvar
in the example before:
DECLARE @LastInsertedID int
SELECT @LastInsertedID = SCOPE_IDENTITY()
:setvar LastInsertedID @LastInsertedID
GO
PRINT $(LastInsertedID)
It now gives you a error message saying: Must declare the scalar variable @LastInsertedID
.
Again :setvar
does not save the actual value of the variable @LastInsertedID
but furthermore a reference to the variable itself, which of course does not exist in that given context anymore.
My question now is, how do I use :setvar
properly or how can I solve this issue differently with the desired output?
I appreciate any help or input!