2 dongfengcool dongfengcool 于 2014.09.23 16:46 提问

将查询到的结果分别赋值给不同的变量??

declare @tablename varchar(100)='Bsheet',--示例表
@keyname varchar(80),
@keyname1 varchar(80),
@keyname2 varchar(80),
@keyname3 varchar(80),
@keyname4 varchar(80),
@keyname5 varchar(80),
@i int =0,
@sql1 Nvarchar(1000);

declare C cursor fast_forward READ_ONLY
for
select rtrim(b.name) as Keycolname --这段代码得到这个表所有主键>2
from sysobjects a,syscolumns b ,

(
select g.id,g.colid,g.keyno

from sysindexes f,sysindexkeys g

where f.id = g.id and f.indid = g.indid and f.indid > 0

and f.indid < 255 and (f.status & 2048)<>0
) h
where a.id = b.id

and a.id = object_id(@tablename)
and b.id = h.id

and b.colid = h.colid

order by h.keyno
open C
fetch next from C into @keyname;
while @@FETCH_STATUS =0
begin
set @i = @i+1;
set @sql1=N'set @keyname'+CAST(@i as varchar(8))+'='+@keyname
exec (@sql1);
fetch next from C into @keyname;
end

close C
deallocate C;

select @keyname1,@keyname2,@keyname3,@keyname4,@keyname5

--这个游标就是想实现类似这样的功能:
set @keyname1=@keyname;
set @keyname2=@keyname;
set @keyname3=@keyname;
set @keyname4=@keyname;
set @keyname5=@keyname;

结果他报错了。请问高手如何实现??其他方法不限。

Csdn user default icon
上传中...
上传图片
插入图片