需要的是类似下面的结果(这个肯定是不行的): select * from (select name from sysobjects where type='U' and name like 'Y%') where barcode='1234' [解决办法] 先找出表名,再根据表去找数据 [解决办法]
declare @tb nvarchar(50),@sql nvarchar(2000) select @tb=name from sysobjects where type='U' and name like 'Y%' set @sql='select * from '+@tb+' where barcode=''1234'' ' exec(@sql)
declare @sql varchar(max) set @sql = '' select @sql = @sql+ ' select * from '+ t.name +'where barcode=''1234'' ' from sysobjects as t join sys.all_columns as c on t.id = c.object_id and t.type='U' and c.name ='barcode' and t.name like 'Y%' exec (@sql)
------解决方案--------------------
--sql 2008有自己的目录视图 use dbname go select * from sys.tables where name like 'y%'