Search code examples
sql-serversql-server-data-toolssqlcmd

How to change SSDT SQLCMD variable in pre-script?


I am trying to find out how to change SQLCMD variable on the fly and I couldn't get it working.

The goal is to get value from the SELECT and assign SQLCMD variable with that value.

I've tried:

1)

:servar myVariable 
SELECT @myVariable = 1

2) Tried to put the value of the file with :OUT but it says that:

Error 1 72006: Fatal scripting error: Command Out is not supported.


Solution

  • You need to declare a temporary sql @variable and assign it value from select.

    Then initialize sqlcmd variable using sql @variable.

    DECLARE @sqlVar CHAR(1)
    
    SELECT @sqlVar = '1'
    :setvar myVar @sqlVar
    SELECT $(myVar) as value
    
    SELECT @sqlVar = '2'
    :setvar myVar @sqlVar
    SELECT $(myVar) as value