我想在运行时动态确定存储过程中要更新的列
我有一个BshForecast
具有以下属性的表:
projectId, JobTypeId, Year, JanDone, FebDone, MarDone... (for all months)
我有这个不工作的存储过程
CREATE PROCEDURE [dbo].[SP_Update_Done_Last_Month2]
@Project_ID INT,
@Job_Type_ID INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @LAST_MONTH INT
SET @LAST_MONTH = DATEPART(MM,DATEADD(MM, -1, GETDATE()))
UPDATE BshForecast
Set (CASE @LAST_MONTH
WHEN 1 THEN JanDone
WHEN 2 THEN FebDone
WHEN 3 THEN MarDone
WHEN 4 THEN AprDone
WHEN 5 THEN MayDone
WHEN 6 THEN JunDone
WHEN 7 THEN JulDone
WHEN 8 THEN AugDone
WHEN 9 THEN SepDone
WHEN 10 THEN OctDone
WHEN 11 THEN NovDone
WHEN 12 THEN DecDone
END) = 0
WHERE
(ProjectId = @Project_ID)
and (JobTypeId = @Job_Type_ID)
and (Year = DATEPART(YY, DATEADD(MM, -1, GETDATE())))
END
你认为似乎是什么问题?这样做的正确方法是什么?