SQL如何查询有记录的所有表
select a.name as 表名,b.name as 列名 from sysobjects a left join syscolumns b on (a.id=b.id) where b.name='cInvCode' and exists (select top 1 * from a.name)
如上查询代码,从系统表中查询所有包含列(cInvCode)的有记录的表,现在这条语句的错误是:
对象名 'a.name' 无效。
不知怎样能达到这种目的,
------解决方案--------------------EXEC [sys].[sp_MSforeachtable] 'if(select count(*) from ?)>0 print ''?'''
------解决方案--------------------含 cInvCode 列的表名:
SELECT
a.name AS [表名]
,b.name AS [列名]
FROM sys.sysobjects AS a
INNER JOIN sys.syscolumns AS b ON a.id=b.id
WHERE EXISTS(SELECT 1 FROM sys.sysindexes AS x
WHERE x.id=a.id
AND x.rows>0
)
AND a.type='U'
AND b.name='cInvCode'