自定义一个学生表(学号、姓名和性别),利用游标
读取表中数据并输出。
*/
--创建一个学生表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)
)
--插入数据:
insert student
select 1001,'tracy',1 union all
select 1002,'lily',2 union all
select 1003,'kobe',1 union all
--声明游标:
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
读取表中数据并输出。
*/
--创建一个学生表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)
)
--插入数据:
insert student
select 1001,'tracy',1 union all
select 1002,'lily',2 union all
select 1003,'kobe',1 union all
--声明游标:
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
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货