0

我有一个这样的存储过程

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 语句给出错误,例如 Msg 156、Level 15、State 1、Procedure FBCount、Line 84 关键字“CASE”附近的语法不正确。消息 156,级别 15,状态 1,过程 FBCount,第 96 行关键字“end”附近的语法不正确。

请帮我

先感谢您

夏拉

4

1 回答 1

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'

[但说实话肯定有比使用游标更好的方法]

于 2013-09-16T06:03:06.130 回答