declare @a int, @b int ,@c int --变量
declare test_1_cursor cursor SCROLL --声明游标
for select * from test_1
open test_1_cursor --打开游标
fetch last from test_1_cursor --最后一行数据
fetch RELATIVE -2 from test_1_cursor --当前行的第2行。
close test_1_cursor --结束游标
deallocate test_1_cursor --释放游标为啥,FETCH 取出来的变量不能INTO到变量中,在下面的又如何判断@@cursor_rows
declare test_1_cursor cursor SCROLL --声明游标
for select Col1,COl2 from test_1
declare @Col1 int,@Col2 int
open test_1_cursor --打开游标
fetch last from test_1_cursor into @Col1,@Col2 --最后一行数据
select @Col1,@Col2 --看效果
fetch RELATIVE -2 from test_1_cursor into @Col1,@Col2 --当前行的第2行。
select @Col1,@Col2 --看效果
close test_1_cursor --结束游标
deallocate test_1_cursor --释放游标
declare test_1_cursor cursor SCROLL --声明游标
for select Col1,COl2 from test_1
declare @Col1 int,@Col2 int
open test_1_cursor --打开游标
fetch last from test_1_cursor into @Col1,@Col2 --最后一行数据
select @Col1,@Col2 --看效果
fetch RELATIVE -2 from test_1_cursor into @Col1,@Col2 --当前行的第2行。
select @Col1,@Col2 --看效果
close test_1_cursor --结束游标
deallocate test_1_cursor --释放游标
declare @a int, @b int ,@c int --变量
declare test_1_cursor cursor SCROLL --声明游标
for select Col1,COl2 from test_1
declare @Col1 int,@Col2 int
open test_1_cursor --打开游标
fetch last from test_1_cursor into @Col1,@Col2 --最后一行数据
insert into @t select @Col1,@Col2 --看效果
fetch RELATIVE -2 from test_1_cursor into @Col1,@Col2 --当前行的第2行。
insert into @t select @Col1,@Col2 --看效果
close test_1_cursor --结束游标
deallocate test_1_cursor --释放游标
select * from @t
自定义一个学生表(学号、姓名和性别),利用游标
读取表中数据并输出。
*/
--创建一个学生表student:
go
IF OBJECT_ID('student') is not null
drop table student
go
create table student(
id int ,
name varchar(10),
gender int check(gender=1 or gender=2)
)
--使用批处理插入一些数据:
go
insert into student values(1001,'tracy',1)
insert into student values(1002,'lily',2)
insert into student values(1003,'kobe',1)
insert into student values(1004,'lucy',2)
insert into student values(1005,'nash',1)declare cur cursor for select *from student
declare @id int,@name varchar(10),@gender int
open cur
fetch next from cur into @id,@name,@gender
while @@fetch_status=0
begin
print ltrim(str(@id))+','+@name+','+ltrim(str(@gender))
fetch next from cur into @id,@name,@gender
end
close cur
deallocate cur
declare @Col1 int,@Col2 int 注意字段和接收变量的类型要一致
CSDN,又2了,我提交了几次,嫩是没刷新过来