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 然後動態執行

解决方案 »

  1.   

    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)兩個單引號表示一個單引號
      

  2.   

    也可:
    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 ')