title: Override an SQLCMD variable inside a T-SQL script
body: <p>I have written this T-SQL code that I ran in SQLCMD Mode in SSMS:</p>
  
  <pre><code>:setvar ENV "Table1"
  
  DECLARE @DomainPrefix NVARCHAR(50) = N'$(ENV)';
  
  IF(@DomainPrefix = N'Table1')
  BEGIN
      SET @DomainPrefix = 'Table2';
  END;
  
  :setvar Prefix @DomainPrefix
  
  PRINT $(Prefix)
  
  SELECT * FROM [DATA].[mySchema].[$(Prefix)]
  </code></pre>
  
  <p>The given output is the following:</p>
  
  <blockquote>
    <p>Table2 
    Msg 208, Level 16, State 1, Line 12 Invalid object name
    'DATA.mySchema.@DomainPrefix'.</p>
  </blockquote>
  
  <p>I don't understand why it is not replacing $(DomainPrefix) by the value 'Table2' that I am able to print on the previous line.</p>