设计问题 select 字段a from tb where 。 union all select 字段b from tb where 。 union all select 字段c from tb where 。 union all select 字段d from tb where 。union all select 字段e from tb where 。 ...............
exec sp_msforeachtable N' declare @tableName nvarchar(256), @sql nvarchar(4000),@value int set @tableName=parsename(''?'',1) set @sql=N'''' set @value=100select @sql=@sql+N'' union all select ''''''+@tableName+'''''' as tableName from ''+@tableName+'' where ''+name+''=''''''+rtrim(@value)+'''''''' from syscolumns where id=object_id(@tableName) and type_name(xtype)=''int''set @sql=stuff(@sql,1,10,'''') --print @sql exec(@sql) '
select 字段a from tb where 。
union all
select 字段b from tb where 。
union all
select 字段c from tb where 。
union all
select 字段d from tb where 。union all
select 字段e from tb where 。
...............
declare @tableName nvarchar(256), @sql nvarchar(4000),@value int
set @tableName=parsename(''?'',1)
set @sql=N''''
set @value=100select @sql=@sql+N'' union all select ''''''+@tableName+'''''' as tableName from ''+@tableName+'' where ''+name+''=''''''+rtrim(@value)+''''''''
from syscolumns
where id=object_id(@tableName) and type_name(xtype)=''int''set @sql=stuff(@sql,1,10,'''')
--print @sql
exec(@sql)
'
不需要union的.