枚举transact-sql列

枚举transact-sql列,sql,enumerate,Sql,Enumerate,有人知道如何使用transact-sql枚举transact-sql结果集中的列类型吗。我想做这样的事情(伪代码): 但我需要从transact-sql内部执行,我事先不知道table1的结构是什么。 有人知道怎么做吗?我正在使用MS SQL 2005。简而言之,我希望将表1中特定记录的所有uniqueidentifier值写入#表2中。 谢谢 警告,未测试: Create Table #Cols(ColName SysName) Declare @More Bit Declare CCol C

有人知道如何使用transact-sql枚举transact-sql结果集中的列类型吗。我想做这样的事情(伪代码):

但我需要从transact-sql内部执行,我事先不知道table1的结构是什么。 有人知道怎么做吗?我正在使用MS SQL 2005。简而言之,我希望将表1中特定记录的所有uniqueidentifier值写入#表2中。 谢谢

警告,未测试:

Create Table #Cols(ColName SysName)
Declare @More Bit
Declare CCol Cursor Local Fast_Forward For Select Column_Name From Information_Schema.Columns Where Table_Name = 'Table1' And Data_Type = 'UniqueIdentifier'
Declare @CCol SysName
Declare @SQL National Character Varying(4000)

Set @More = 1
Open CCol

While (@More = 1)
Begin
  Fetch Next From CCol Into @CCol
  If (@@Fetch_Status != 0)
    Set @More = 0
  Else
  Begin
    Set @SQL = N'Insert Into #Table2(ID) Select [' + @CCol + N'] From Table1'
    Execute (@SQL)
  End
End

Close CCol
Deallocate CCol

...
警告,未测试:

Create Table #Cols(ColName SysName)
Declare @More Bit
Declare CCol Cursor Local Fast_Forward For Select Column_Name From Information_Schema.Columns Where Table_Name = 'Table1' And Data_Type = 'UniqueIdentifier'
Declare @CCol SysName
Declare @SQL National Character Varying(4000)

Set @More = 1
Open CCol

While (@More = 1)
Begin
  Fetch Next From CCol Into @CCol
  If (@@Fetch_Status != 0)
    Set @More = 0
  Else
  Begin
    Set @SQL = N'Insert Into #Table2(ID) Select [' + @CCol + N'] From Table1'
    Execute (@SQL)
  End
End

Close CCol
Deallocate CCol

...

嗯,要做到这一点并不容易。这里有一个有点难看的代码,它可以满足您的需要。它基本上接受未知的输入查询,在tempdb中创建表,枚举guid列并将它们转储到temp table#guid中。

嗯,要做到这一点并不容易。这里有一个有点难看的代码,它可以满足您的需要。它基本上接受未知的输入查询,在tempdb中创建表,枚举guid列并将它们转储到temp table#guid中。

谢谢你,斯图,我请客了。遗憾的是,如果不恢复到Information_Schema.Columns,就无法完成此操作,但我想这并不是一个很大的缺点。再次感谢。谢谢斯图,我请客了。遗憾的是,如果不恢复到Information_Schema.Columns,就无法完成此操作,但我想这并不是一个很大的缺点。再次感谢。非常感谢帕维尔,看起来也不错,希望你不介意我给斯图答案。不,我不在乎声誉或那些数字的含义,我喜欢解决难题:)非常感谢帕维尔,看起来也不错,希望你不介意我给斯图答案。不,我不在乎声誉或那些数字的含义,我喜欢解谜:)
declare @sourceQuery varchar(max)
set @sourceQuery = 'select 1 as IntCol, newid() as GuidCol1, newid() as GuidCol2, newid() as GuidCol3' 
declare @table varchar(255) = replace( cast( newid() as varchar(40)), '-', '' )


print @table
declare @script varchar(max)
set @script = '
select *
into tempdb..[' + @table + ']
from ( ' + @sourceQuery + ' ) as x
'
exec( @script )


create table #guids
(
    G uniqueidentifier not null
)

declare cr cursor fast_forward read_only for
select c.name
from tempdb.sys.objects as s
inner join tempdb.sys.columns as c
    on s.object_id = c.object_id
where s.name = @table
    and c.system_type_id = 36 -- guid

declare @colName varchar(256)

open cr
fetch next from cr into @colName
while @@FETCH_STATUS = 0
begin
    set @script = ' 
    insert into #guids(G)
    select ' + @colName + ' from (' + @sourceQuery + ') as x '

    exec( @script )

    fetch next from cr into @colName
end

close cr
deallocate cr

select * from #guids

exec( 'drop table tempdb..[' + @table + ']' )
drop table #guids