title: Dynamic SQL in SQL Server: Executing a string through sp_sqlexec @string VS using a pure SQL query - why does stored string not work?
body: <pre><code>DECLARE @Description NVARCHAR(128) = N'MS_Description'
  --Just for good measure
  SET @Description = N'MS_Description'
  </code></pre>
  
  <p>The first statement is me trying to use <code>sp_sqlexec</code> by storing my SQL with dynamic parameters in a string:</p>
  
  <pre><code>SELECT @query = @query + 'SELECT * FROM TABLE 
                            WHERE COLUMN = ' + @Description
  EXEC sp_sqlexec @query
  </code></pre>
  
  <p>The above doesn't work, I get the following error: </p>
  
  <blockquote>
    <p>Invalid column name 'MS_Description'</p>
  </blockquote>
  
  <p>When I debug through this, the statement is exactly the same as the below query, that works and returns the results that I want.</p>
  
  <pre><code>SELECT * 
  FROM table 
  WHERE column = @Description
  </code></pre>
  
  <p>Could someone please explain the difference and why the below works if it is the exact same thing that's stored in <code>@query</code>? Thank you</p>