日期:2014-05-17 浏览次数:20598 次
DECLARE @sql VARCHAR(MAX)
SET @sql=''
;
WITH a1 AS
(
SELECT b.name
FROM syscolumns a
INNER JOIN sysobjects b ON a.id=b.id
where UPPER(a.name)='COU' AND b.xtype='U'
)
SELECT @sql=@sql+CASE WHEN @sql='' THEN '' ELSE ' union all ' END + 'select top 1 '''+
+name+''' name from '+name+' where COU=''000001'''
FROM a1
EXEC(@sql)
--簡化一下:
DECLARE @sql VARCHAR(MAX)
SET @sql=''
;
SELECT @sql=@sql+CASE WHEN @sql='' THEN '' ELSE ' union all ' END + 'select top 1 '''+
+b.name+''' name from '+b.name+' where COU=''000001'''
FROM syscolumns a
INNER JOIN sysobjects b ON a.id=b.id
where UPPER(a.name)='COU' AND b.xtype='U'
EXEC(@sql)