drop table #pkindcols
create table aaa ( i int, j int, c char(35), primary key ( i, j ) )
convert( varchar(255), "" ) cols
into #pkindcols
from sysindexes i, sysobjects o
create unique index pkindcols_idindid on #pkindcols ( id, indid )
go
declare colnames
cursor for
select id, indid, keycnt, objname
from #pkindcols
for update
go
declare @i int, @id int, @indid int, @keycnt int, @objname char(30), @txt varchar(30)
open colnames
fetch from colnames into @id, @indid, @keycnt, @objname
while ( @@sqlstatus = 0 )
begin
select @i=1
while ( @i <= @keycnt )
begin
select @txt=ltrim(rtrim(index_col( @objname, @indid, @i )))
select @txt
update #pkindcols set cols=cols+ltrim(rtrim(index_col( @objname, @indid, @i )))+"|"
where indid=@indid AND id=@id
select @i=@i+1
end
fetch from colnames into @id, @indid, @keycnt, @objname
end
close colnames
deallocate colnames
select cols, * from #pkindcols
go
drop table information_schema_columns