declare @str nvarchar(2000)set @str='select c.name cname,o.name tname,t.name type,c.length,c.isnullable,c.domain,c.status
into [pubs].dbo.d1table
from '+ @d1 +'.dbo.syscolumns c , '+ @d1 +'.dbo.sysobjectso,'+ @d1 +'.dbo.systypes t
where o.xtype='u' and c.id=o.id and c.xtype=t.xtype
order by c.id 'exec @str即轉換成STR 然後動態執行
into [pubs].dbo.d1table
from '+ @d1 +'.dbo.syscolumns c , '+ @d1 +'.dbo.sysobjectso,'+ @d1 +'.dbo.systypes t
where o.xtype='u' and c.id=o.id and c.xtype=t.xtype
order by c.id 'exec @str即轉換成STR 然後動態執行
into [pubs].dbo.d1table
from '+ @d1 +'.dbo.syscolumns c , '+ @d1 +'.dbo.sysobjectso,'+ @d1 +'.dbo.systypes t
where o.xtype=''u'' and c.id=o.id and c.xtype=t.xtype
order by c.id 'exec (@str)兩個單引號表示一個單引號
exec('select c.name cname,o.name tname,t.name type,c.length,c.isnullable,c.domain,c.status
into [pubs].dbo.d1table
from '+ @d1 +'.dbo.syscolumns c , '+ @d1 +'.dbo.sysobjectso,'+ @d1 +'.dbo.systypes t
where o.xtype='u' and c.id=o.id and c.xtype=t.xtype
order by c.id ')