title: TSQL dynamic adding of columns in stored procedure
body: <p>Hi I am writing a large stored procedure, which creates a dynamic report table, of n columns in size, the first 6 are constant the remainder depend on a few arguments passed to the procedure to create the table with the required columns.</p>
  
  <p>The problem that I am having is with the following TSQL</p>
  
  <pre><code>DECLARE @columnname VARCHAR(50)
  SET @columnname = 'on_' + @description
  
  IF NOT EXISTS(SELECT * FROM syscolumns WHERE id = OBJECT_ID('reports')
         AND NAME = @columnname)
  BEGIN
        ALTER TABLE reports ADD @columnname VARCHAR(50) NULL
  END
  </code></pre>
  
  <p>I am getting syntax errors with this at the @columnname in the ALTER TABLE statement of the above code.</p>
  
  <p>Also as I am new to this, I am not sure if this is the best way to do this, or if there are better ways in TSQL to generate the required dynamic table.</p>