View Single Post

   
  #4 (permalink)  
Old 05-16-2008, 02:39 PM
Plamen Ratchev
 
Posts: n/a
Default Re: create dynamic variable in procedure

You cannot use variables for column names in dynamic SQL. You would have to
concatenate the SQL as a string to add the columns, and then pass the other
parameters and execute.

DECLARE @sql NVARCHAR(2000);

SET @sql = N'
UPDATE tblAnalysisScores
SET WEAQScore' + CAST(@Interval AS CHAR(1)) + ' = @WEAQScore, ' +
' MissingWEAQ' + CAST(@Interval AS CHAR(1)) + ' = @MissingWEAQ
WHERE SubID = @SubID';

DECLARE @params NVARCHAR(100);

SET @params = N'@WEAQScore INT, @MissingWEAQ INT, @SubID INT';

EXEC sp_executesql @sql, @params, @WEAQScore, @MissingWEAQ, @SubID

Read Erland Sommarskog's article on dynamic SQL:
http://www.sommarskog.se/dynamic_sql.html

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Reply With Quote