gpt4 book ai didi

SQL 服务器 : Get All views from All databases

转载 作者:行者123 更新时间:2023-12-02 17:40:28 25 4
gpt4 key购买 nike

实际上我可以获得包含某个表的所有数据库的列表:

SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE'
THEN OBJECT_ID(QUOTENAME(name) + '.[dbo].[TABLE_NAME]', 'U')
END IS NOT NULL

但现在我想对 View 做一些类似的事情,以便列出数据库中的所有 View 。

你能帮我解决这个问题吗?

问候。

最佳答案

CREATE TABLE #v(d SYSNAME, v NVARCHAR(512));

DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';

SELECT @sql = @sql + N'INSERT #v(d,v)
SELECT ''' + REPLACE(name, '''','''''') + ''',
QUOTENAME(s.name) + ''.'' + QUOTENAME(v.name)
FROM ' + QUOTENAME(name) + '.sys.views AS v
INNER JOIN ' + QUOTENAME(name) + '.sys.schemas AS s
ON v.[schema_id] = s.[schema_id]
WHERE v.is_ms_shipped = 0;
'
FROM sys.databases
WHERE [state] = 0;

EXEC sp_executesql @sql;

SELECT [database] = d, [view] = v FROM #v ORDER BY d,v;

DROP TABLE #v;

关于SQL 服务器 : Get All views from All databases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21464031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com