日期:2014-05-18 浏览次数:20750 次
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 --释放游标
自定义一个学生表(学号、姓名和性别),利用游标 读取表中数据并输出。 */ --创建一个学生表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