34,837
社区成员




exec('select table1.*'+select (select ','+cast(column_name as varchar) as [text()]
from information_schema.columns
where table_name='table2' and column_name <>'id'
for xml path(''))+' from table1,table2 where table1.id=table2.id '
)
declare @sql varchar(8000)
select @sql='select table1.*'+select (select ','+cast(column_name as varchar) as [text()]
from information_schema.columns
where table_name='table2' and column_name <>'id'
for xml path(''))+' from table1,table2 where table1.id=table2.id '
exec (@sql)
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+name
from syscolumns where id=object_id('bm')
and name <>'bmid'
exec('select '+@sql+' from bm')
declare @s nvarchar(4000)
select
@s=isnull(@s+',','')+'t1.'+quotename(Name)
from
syscolumns
where
ID=object_id('table1')
order by colid
select
@s=isnull(@s+',','')+'t2.'+quotename(Name)
from
syscolumns
where
ID=object_id('table2') and Name<>'ID'
order by colid
exec('select '+@s+' from table1 t1,table2 t2 where t1.ID=t2.ID')
declare @col varchar(1000)
select @col=isnull(@col+',','')+name from syscolumns where id=object_id('table2')
where name!='id'
declare @sql varchar(8000)
set @sql='select a.*,'+@col+' from table1 a,table2 b where a.id=b.id'
exec(@sql)