主题:关于游标的一个问题,大家看过来了
1: 写一个存储过程实现求学生的平均总成绩(必须用游标实现)
if exists(select * from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
create procedure st_cursor @st_cursor cursor varying output
as
begin
declare @aver int
set @st_cursor=cursor forward_only static for
select @aver=(select avg(成绩) from xs_kc
group by 学号)
return @aver
end
open @st_cursor
go
declare @mycursor cursor
execute st_cursor @st_cursor=@mycursor output
while (@@fetch_status=0) /*上一个获取成功*/
begin
fetch next from @mycursor
end
close @mycursor
deallocate @mycursor
出错信息:
在 游标声明 中不允许使用 变量赋值。
但是这个书上的例子为什么编译通过了呢?
if exists(select * from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
create procedure st_cursor @st_cursor cursor varying output
as
set @st_cursor=cursor forward_only static for
select * from xs
open @st_cursor
go
declare @mycursor cursor
execute st_cursor @st_cursor=@mycursor output
while (@@fetch_status=0)/*上一个获取成功*/
begin
fetch next from @mycursor
end
close @mycursor
deallocate @mycursor
if exists(select * from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
create procedure st_cursor @st_cursor cursor varying output
as
begin
declare @aver int
set @st_cursor=cursor forward_only static for
select @aver=(select avg(成绩) from xs_kc
group by 学号)
return @aver
end
open @st_cursor
go
declare @mycursor cursor
execute st_cursor @st_cursor=@mycursor output
while (@@fetch_status=0) /*上一个获取成功*/
begin
fetch next from @mycursor
end
close @mycursor
deallocate @mycursor
出错信息:
在 游标声明 中不允许使用 变量赋值。
但是这个书上的例子为什么编译通过了呢?
if exists(select * from sysobjects where name='st_cursor' and type='p')
drop procedure st_cursor
go
create procedure st_cursor @st_cursor cursor varying output
as
set @st_cursor=cursor forward_only static for
select * from xs
open @st_cursor
go
declare @mycursor cursor
execute st_cursor @st_cursor=@mycursor output
while (@@fetch_status=0)/*上一个获取成功*/
begin
fetch next from @mycursor
end
close @mycursor
deallocate @mycursor