更新案例语句在T-SQL中不起作用

时间:2013-09-16 05:57:22

标签: sql-server tsql

我有一个像这样的存储过程

CREATE TABLE  #@TTableName
(Grp varchar(100),
TotalFB int,
Excellent int,
ExcellentPerc float,
AAvg int,
AAvgPerc float,
Average int,
AveragePerc float,
BAvg int,
BAvgPerc float,
Poor int,
PoorPerc float
)

INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Parking',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Enquiry',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Registration',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('WaitingTime',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Pharmacy',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Laboratory',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Radiology',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Receptionist',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Nurse',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Consultants',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Others',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Cleanliness',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Signage',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAvgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Ambience',0,0,0,0,0,0,0,0,0,0,0)
INSERT INTO #@TTableName(Grp,TotalFB,Excellent,ExcellentPerc,AAvg,AAVgPerc,Average,AveragePerc,BAvg,BAvgPerc,Poor,PoorPerc) values ('Lift',0,0,0,0,0,0,0,0,0,0,0)

begin
declare Code_Cursor insensitive cursor for 
    select *
    FROM FB
    where EDate >= convert(datetime,@FrDate) and EDate <= convert(datetime,@ToDate) --between convert(datetime,@FrDate) and convert(datetime,@ToDate) 
end

Open Code_Cursor
fetch next from Code_Cursor into @Parking,@Enquiry,@Registration,@WaitingTime,@Pharmacy,@Laboratory,@Radiology,@Receptionist,@Nurse,@Consultants,@Others,@Cleanliness,@Signage,@Ambience,@Lift,@AnyOther
while @@fetch_status =0
begin

UPDATE #@TTableName SET

    CASE @Parking

        WHEN  5 THEN Excellent=Excellent + 1 
        WHEN  4 THEN AAvg=AAvg + 1
        WHEN  3 THEN Average=Average + 1
        WHEN  2 THEN BAvg=BAvg + 1
        WHEN  1 THEN Poor=Poor + 1

    END
where @Grp='Parking'

fetch next from Code_Cursor into @Parking,@Enquiry,@Registration,@WaitingTime,@Pharmacy,@Laboratory,@Radiology,@Receptionist,@Nurse,@Consultants,@Others,@Cleanliness,@Signage,@Ambience,@Lift,@AnyOther
end
close Code_Cursor
deallocate Code_Cursor

--UPDATE #@TTableName SET Meddesc = (SELECT Description
--from Medtype  WHERE #@TTableName.cardtype = MedType.Type)
--FROM #@TTableName,MedType

select * from #@TTableName
GO

其中update case语句给出错误,如消息156,级别15,状态1,过程FBCount,第84行 关键字“CASE”附近的语法不正确。 信息156,第15级,状态1,程序FBCount,第96行 关键字'end'附近的语法不正确。

请帮帮我

提前谢谢

shaila

1 个答案:

答案 0 :(得分:2)

您的尝试不起作用,因为未定义UPDATE语法

你可以这样做:

UPDATE #@TTableName 
SET 
    Excellent =  CASE WHEN @Parking = 5 THEN Excellent + 1 ELSE Excellent END,
    AAvg      =  CASE WHEN @Parking = 4 THEN AAvg + 1 ELSE AAvg END,
    -- etc....

where @Grp = 'Parking'

[但说实话,必须有比使用光标更好的方法]