title: Dynamically adding columns via parameterised stored procedure
body: <p>I have a Project table which has Poject Start and Project End dates. I need to add the columns as Quarters into a new table Planned Cost where Quarters should be between the Project Start and End Dates. </p>
  
  <p>Have wrote a procedure to dynamically add the columns based on the parameters which we are passing- Project and Business name. But the code is not working and giving the error.</p>
  
  <p>Code :</p>
  
  <pre><code>CREATE PROCEDURE ****
  @Project_Name NVARCHAR(100) output, 
  @Business NVARCHAR(100) output 
  AS 
  SET nocount ON 
  
      DECLARE @column NVARCHAR(20), 
              @SQL    NVARCHAR(1000) 
      DECLARE @ParamDefinition AS NVARCHAR(2000) 
  
      SET @column = N'select Q +cast(FLOOR(((12 + MONTH(Engagement_Master_FPA.[planned start date])) % 12) / 3 ) + 1  as varchar(4))+_+CAST(year(Engagement_Master_FPA.[planned start date]) AS varchar(4)) + from Engagement_Master_FPA where Engagement_Master_FPA.project=' 
                    + @Project_Name + ' and Business=' + @Business 
      SET @SQL = N'ALTER TABLE [[Planned_Cost_Master_FPA_BKP] ADD ' + @column + ' varchar(50)' 
      SET @ParamDefinition=N'@Project_Name nvarchar(100) ,@Business nvarchar(100)' 
  
      EXECUTE Sp_executesql 
        @SQL, 
        @ParamDefinition, 
        @Project_Name output, 
        @Business output 
  
      SELECT @Project_Name, 
             @Business 
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>Msg 156, Level 15, State 1, Line 5
  Incorrect syntax near the keyword 'select'.
  Msg 102, Level 15, State 1, Line 5
  Incorrect syntax near 'varchar'.
  </code></pre>
  
  <p>(1 row(s) affected)</p>
  
  <p>(1 row(s) affected)</p>