动态 SQL 从 information_schema 获取行
假设我正在数据库中查找特定列,所以我有类似的内容
SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like ‘%employeeid%’
但我也想知道每个表有多少行,有人告诉我可以使用动态 SQL 来完成此操作,所以我现在有了这个
DECLARE
@tableName NVARCHAR(MAX),
@sql NVARCHAR(MAX),
@colName NVARCHAR(MAX);
DECLARE CUR_TABLE CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
OPEN CUR_TABLE
FETCH NEXT FROM CUR_TABLE
INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @colName = '%employeeid%'
SET @sql = 'SELECT COLUMN_NAME, TABLE_NAME, (SELECT COUNT(*) FROM ' + @tableName +') AS ROWS FROM INFORMATION_SCHEMA.COLUMNS where column_name like ' + ''' + @colName + ''';
FETCH NEXT FROM CUR_TABLE
INTO @tableName
END;
CLOSE CUR_TABLE
DEALLOCATE CUR_TABLE
EXEC sp_executesql @sql
但这并不不起作用,我想做的是使用我正在查找的列、表名称和表中的行数查询表。
我该如何解决这个问题?
Let’s say I’m looking for a specific column in my database so I have something like this
SELECT COLUMN_NAME, TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME like ‘%employeeid%’
But I also want to know how many rows each table has, I was told I can do this using Dynamic SQL so I have this now
DECLARE
@tableName NVARCHAR(MAX),
@sql NVARCHAR(MAX),
@colName NVARCHAR(MAX);
DECLARE CUR_TABLE CURSOR FOR
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS
OPEN CUR_TABLE
FETCH NEXT FROM CUR_TABLE
INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @colName = '%employeeid%'
SET @sql = 'SELECT COLUMN_NAME, TABLE_NAME, (SELECT COUNT(*) FROM ' + @tableName +') AS ROWS FROM INFORMATION_SCHEMA.COLUMNS where column_name like ' + ''' + @colName + ''';
FETCH NEXT FROM CUR_TABLE
INTO @tableName
END;
CLOSE CUR_TABLE
DEALLOCATE CUR_TABLE
EXEC sp_executesql @sql
But this doesn't work, What I'm trying to do is query a table with the column I am looking for, with the table name, and number of rows in the table.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以利用 SQL Server 的动态管理视图快速获取行数*。
查找具有名为“MyColumn”的列及其当前行的所有表:
* 准确,但频繁更新的表可能会出现一些滞后
You can make use of SQL Server's dynamic management views to quickly obtain the row counts*.
Find all tables with a column named 'MyColumn' and their current rows:
* Accurate except for frequently updated tables where there could be some lag
以下使用 INFORMATION_SCHEMA、动态 SQL 和 STRING_AGG() 构建将返回单个结果集的查询。
我通过添加 TABLE_SCHEMA 对其进行了一些概括,以便它可以与 AdventureWorks 数据库一起使用。请参阅此 db<>fiddle 以获取工作演示。还包括对旧版 SQL Server 版本使用 FOR XML 而不是 STRING_AGG 的等效逻辑。
The following uses INFORMATION_SCHEMA, dynamic SQL, and STRING_AGG() to build a query that will return a single result set.
I generalized it a bit by adding TABLE_SCHEMA so that it could be used with the AdventureWorks database. See this db<>fiddle for a working demo. Also included is equivalent logic that uses FOR XML instead of STRING_AGG for older SQL Server versions.
假设您使用的是 SQL Server,这里有一个使用
sp_msforeachtable
的简写方法。Assuming that you are using SQL Server, here is a shorthand way using
sp_msforeachtable
.