您可以使用sys.extended_properties
(info)和動態SQL來獲取所有擴展屬性。
IF OBJECT_ID(N'#extprops') IS NOT NULL DROP TABLE #extprops
CREATE TABLE #extprops (
dbname nvarchar(max),
class_desc sql_variant,
[name] sql_variant,
[value] sql_variant
)
DECLARE @sql nvarchar(max)
SELECT @sql = (
SELECT 'USE '+QUOTENAME([name])+ ' INSERT INTO #extprops SELECT ''' +[name] + ''' as dbname, class_desc, [name], [value] FROM sys.extended_properties;' +CHAR(10)
FROM sys.databases
FOR XML PATH('')
)
PRINT @sql
EXEC sp_executesql @sql
SELECT *
FROM #extprops
輸出:
dbname class_desc name value
msdb DATABASE Microsoft_Management_Utility_Version ___SQLVERSION___NEW___
AdventureWorks DATABASE MS_Description AdventureWorks 2014 Sample OLTP Database
AdventureWorks OBJECT_OR_COLUMN MS_Description Check constraint [ProductLine]='r' OR [ProductLine]='m' OR [ProductLine]='t' OR [ProductLine]='s' OR [ProductLine]='R' OR [ProductLine]='M' OR [ProductLine]='T' OR [ProductLine]='S' OR [ProductLine] IS NULL
AdventureWorks OBJECT_OR_COLUMN MS_Description Stores (including store contacts) that sell Adventure Works Cycles products to consumers.
AdventureWorks OBJECT_OR_COLUMN MS_Description Manufacturing failure reasons lookup table.
AdventureWorks OBJECT_OR_COLUMN MS_Description Primary key for ScrapReason records.
AdventureWorks OBJECT_OR_COLUMN MS_Description Failure description.
...
如果print @sql
,你會看到這樣的查詢:
USE [master] INSERT INTO #extprops SELECT 'master' as dbname, class_desc, [name], [value] FROM sys.extended_properties;
USE [tempdb] INSERT INTO #extprops SELECT 'tempdb' as dbname, class_desc, [name], [value] FROM sys.extended_properties;
USE [model] INSERT INTO #extprops SELECT 'model' as dbname, class_desc, [name], [value] FROM sys.extended_properties;
之後,你可以在#extprops
表使用任何過濾器。
屬性'x'是什麼?任何示例?關於數據庫的主要數據存儲在這裏'SELECT * FROM sys.databases' – gofr1
sql server中的每個數據庫都具有作爲鍵值對的擴展屬性 我希望使用鍵「x」的擴展屬性。 – Motaz
我懂了!希望我的回答會幫助你。 – gofr1