title: Dynamic sql query input and output values pass in variables
body: <p>pass column name &amp; value dynamically and get result of query into another variable. </p>
  
  <pre><code>@ColumnName , @SKU_ID are input variables, output data store into @ColumnValue variable.
  DECLARE @ColumnName
  char(50)
  DECLARE @SKU_ID
  varchar(50)
  DECLARE @ColumnValue
  varchar(150)
  DECLARE @Sqlcommand
  nvarchar(1000)
  DECLARE @ColumnData
  varchar(50)
  
  SET @ColumnName = 'Color_Code'
  SET @SKU_ID  = 'W16933'
  
  SET @Sqlcommand = 'SELECT @ColumnData ='+@ColumnName+ 'FROM Stagetable WHERE SKU_ID = @SKU_ID' 
  exec sp_executesql @Sqlcommand, N'ColumnData varchar(50) out' , @ColumnValue out
  </code></pre>
  
  <h1>I'm getting below Error</h1>
  
  <p>Msg 102, Level 15, State 1, Line 1
  Incorrect syntax near 'ColumnData'.
  Msg 137, Level 15, State 1, Line 1
  Must declare the scalar variable "@ColumnData".
  please help me out. </p>