我有一个Project表,其中包含Poject Start和Project End日期。我需要将列作为Quarters添加到新表Planned Cost中,Quarters应该在Project Start和End Dates之间。
已经编写了一个程序,根据我们传递的参数动态添加列 - 项目和业务名称。但是代码没有工作并且给出了错误。
代码:
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
错误:
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'.
(1行受影响)
(1行受影响)