Create PROCEDURE my_sql
AS
declare @id int,@loginname varchar(20),@website varchar(100)
declare rs cursor for select top 50 id,loginname,website from union_user
open rs
fetch next from rs
into @id,@loginname,@website
while @@fetch_status=0
begin
print convert(varchar,@id) + ',' + convert(varchar,@loginname) + ',' + convert(varchar,@website)
fetch next from rs
into @id,@loginname,@website
end
close rs
deallocate rs
GO
----------------------
上面的declare rs cursor for 后面的查询语句放在declare rs cursor for 前面就定义好了,然后在
declare rs cursor for 后再如:exec(@sql) 这样写。
改后:
Create PROCEDURE my_sql
AS
declare @id int,@loginname varchar(20),@website varchar(100),@sql nvarchar(500)
set @sql='select top 50 id,loginname,website from union_user'
declare rs cursor for
exec(@sql)
open rs
fetch next from rs
into @id,@loginname,@website
while @@fetch_status=0
begin
print convert(varchar,@id) + ',' + convert(varchar,@loginname) + ',' + convert(varchar,@website)
fetch next from rs
into @id,@loginname,@website
end
close rs
deallocate rs
GO
为什么不行。因为我查询还在多个判断。。所以需要单独处理。。
该怎么改????????????