--关键字用[]
select * from [User]
use SHMSDB
declare @i int
declare @name varchar
declare usercursor cursor
for select [username] from [user]
open usercursor
select @i=count(*) from [user]
select @name=UserName from [user]
--读取下一行数据把读取的数据放在变量中
fetch next from usercursor
--是系统关键字代表游标取值的状态等于0表示取到了一行数据 否则表示最后一行数据读完了!
while @@fetch_status=0 and @i>1
begin
fetch next from usercursor into @name
set @i=@i-1
end
close usercursor
--删除游标引用
deallocate usercursor
为什么只能查询到第一条数据,
而for select [username] from [user],
while @@fetch_status=0 and @i>1
begin
fetch next from usercursor set @i=@i-1
end
这样能循环出每一条数据,?
补充问题:
Fetch usercursor into @name
怎么你的这里不要 Fetch next from。。。,我以前没用过游标,谢谢你详解。
还有:
use SHMSDB
declare @i int
declare @index int
declare @name varchar(100)
declare @strName varchar(8000)
select @index=0
declare usercursor cursor
for select [username] from [user]
open usercursor
select @i=count(*) from [user]
--读取下一行数据把读取的数据放在变量中
fetch next from usercursor
--是系统关键字代表游标取值的状态等于0表示取到了一行数据 否则表示最后一行数据读完了!
--while @@fetch_status=0 and @index<@i
--begin
-- fetch next from usercursor into @name
--set @strName=@strName+','
--set @strName=@strName+@name
-- set @i=@i-1
--print cast(@i as varchar)+','+@strName+','+'字符串'
--end
close usercursor
--删除游标引用
deallocate usercursor
qq_花开花谢_0
慕的地10843