我必须编写一个存储过程,在其中给出月份和信用卡号,它计算每月前 10 天进行的每笔交易的 1%,10 到 20 之间的交易的 2%,以及 3%对于 20 以上的交易。我必须使用游标。
我写了这段代码,但是当我尝试运行程序时出现了一些错误
create procedure cardP
/* month ex 1,3,5 etc*/
@minas int,
@cardNo bigint
as
/* creating the cursor*/
DECLARE sinallages CURSOR
FOR SELECT cc_number,day([DateTime]),charged_amount FROM transactions
where cc_number=@cardNo and month([DateTime])=@minas
/* declaring local variables*/
declare @poso int,@karta bigint,@mera int,@pos float,@pos2 float,@pos3 float,
@count int,@counter int
open sinallages
set @count=(select count(cc_number) from transactions where cc_number=@cardNo and month([DateTime])=@minas )
/* get 1st row*/
fetch sinallages into @karta,@mera,@poso
while (/*@@sqlstatus != 2*/@counter<@count)
begin
if day(@mera)<=10
set @pos =@poso+ @poso * 0.01
else
if day(@mera)>10 and day(@mera)<=20
set @pos2 =@poso+ @poso * 0.02
else
if day(@mera) > 20
set @pos3 =@poso+ @poso * 0.03
fetch sinallages into @karta,@mera,@poso
set @counter=@counter+1
end
close sinallages
return
当我调用程序时,我得到
EXEC cardP @minas = 5, @cardNo =4929569752542450
Msg 16915, Level 16, State 1, Procedure cardP, Line 20
A cursor with the name 'sinallages' already exists.
Msg 16922, Level 16, State 1, Procedure cardP, Line 31
游标提取:不允许从数据类型 datetime 到 int 的隐式转换。
谢谢 :) 我现在在存储过程结束时取消分配游标并删除 day()。现在我想打印 pos+pos2+pos3。我使用 print pos+pos2+pos3 但它不打印任何东西。这是为什么 ??
................
set @counter=@counter+1
end
print @pos+@pos2+@pos3
close sinallages
return
DEALLOCATE sinallages;
似乎 hte 变量 po,pos2,pos3 为空?