So, I have a script that adds extended properties, some describing a table, some describing a column. How can I check if the extended property exists before adding it so that the script does not throw an error?
7 回答
第一个脚本检查描述表的扩展属性是否存在:
IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES WHERE [major_id] = OBJECT_ID('Table_Name') AND [name] = N'MS_Description' AND [minor_id] = 0)
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'This table is responsible for holding information.', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Table_Name';
第二个脚本检查描述列的扩展属性是否存在:
IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES WHERE [major_id] = OBJECT_ID('Table_Name') AND [name] = N'MS_Description' AND [minor_id] = (SELECT [column_id] FROM SYS.COLUMNS WHERE [name] = 'Column_Name' AND [object_id] = OBJECT_ID('Table_Name')))
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = N'This column is responsible for holding information for table Table_Name.', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'Table_Name', @level2type = N'COLUMN', @level2name = N'Column_Name';
这是另一种存储过程方法,类似于 Ruslan K.,但不涉及 try/catch 或显式事务:
-- simplify syntax for maintaining data dictionary
IF OBJECT_ID ('dbo.usp_addorupdatedescription', 'P') IS NOT NULL
DROP PROCEDURE dbo.usp_addorupdatedescription;
GO
CREATE PROCEDURE usp_addorupdatedescription
@table nvarchar(128), -- table name
@column nvarchar(128), -- column name, NULL if description for table
@descr sql_variant -- description text
AS
BEGIN
SET NOCOUNT ON;
IF @column IS NOT NULL
IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES
WHERE [major_id] = OBJECT_ID(@table) AND [name] = N'MS_Description'
AND [minor_id] = (SELECT [column_id]
FROM SYS.COLUMNS WHERE [name] = @column AND [object_id] = OBJECT_ID(@table)))
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = @descr,
@level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE',
@level1name = @table, @level2type = N'COLUMN', @level2name = @column;
ELSE
EXECUTE sp_updateextendedproperty @name = N'MS_Description',
@value = @descr, @level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = @table,
@level2type = N'COLUMN', @level2name = @column;
ELSE
IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES
WHERE [major_id] = OBJECT_ID(@table) AND [name] = N'MS_Description'
AND [minor_id] = 0)
EXECUTE sp_addextendedproperty @name = N'MS_Description', @value = @descr,
@level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = @table;
ELSE
EXECUTE sp_updateextendedproperty @name = N'MS_Description', @value = @descr,
@level0type = N'SCHEMA', @level0name = N'dbo',
@level1type = N'TABLE', @level1name = @table;
END
GO
我编写了简单的存储过程来添加或更新扩展属性“MS_Description”:
IF OBJECT_ID ('dbo.usp_addorupdatedescription', 'P') IS NOT NULL
DROP PROCEDURE dbo.usp_addorupdatedescription;
GO
CREATE PROCEDURE usp_addorupdatedescription
@table nvarchar(128), -- table name
@column nvarchar(128), -- column name, NULL if description for table
@descr sql_variant -- description text
AS
BEGIN
SET NOCOUNT ON;
DECLARE @c nvarchar(128) = NULL;
IF @column IS NOT NULL
SET @c = N'COLUMN';
BEGIN TRY
EXECUTE sp_updateextendedproperty N'MS_Description', @descr, N'SCHEMA', N'dbo', N'TABLE', @table, @c, @column;
END TRY
BEGIN CATCH
EXECUTE sp_addextendedproperty N'MS_Description', @descr, N'SCHEMA', N'dbo', N'TABLE', @table, @c, @column;
END CATCH;
END
GO
也许我的回答并没有直接连接到这个问题,但我想指出 MS_Description 实际上是区分大小写的,即使我们用 SQL 添加它。如果我们使用 MS_DESCRIPTION 而不是 MS_Description,它将不会显示在 SMSS 表设计视图中。
就我而言,我必须做这样的事情来删除现有的描述并添加一个正确的描述。
IF EXISTS (
SELECT NULL
FROM SYS.EXTENDED_PROPERTIES
WHERE [major_id] = OBJECT_ID('TableName')
AND [name] = N'MS_DESCRIPTION'
AND [minor_id] = (
SELECT [column_id]
FROM SYS.COLUMNS
WHERE [name] = 'ColumnName'
AND [object_id] = OBJECT_ID('Tablename')
)
)
EXEC sys.sp_dropextendedproperty @name = N'MS_DESCRIPTION'
,@level0type = N'SCHEMA'
,@level0name = N'dbo'
,@level1type = N'TABLE'
,@level1name = N'TableName'
,@level2type = N'COLUMN'
,@level2name = N'ColumnName'
GO
EXEC sys.sp_addextendedproperty @name = N'MS_Description'
,@value = N'Description detail'
,@level0type = N'SCHEMA'
,@level0name = N'dbo'
,@level1type = N'TABLE'
,@level1name = N'TableName'
,@level2type = N'COLUMN'
,@level2name = N'ColumnName'
去
请在上面找到我对 Brian Westrich 答案的扩展,但此版本允许更新和添加表和列上的任何扩展属性,而不仅仅是 MS_Description。此外,它还允许您使用存储过程来添加和更新不同数据库中的扩展属性,因此您只需要在服务器上保存一份副本。
CREATE PROCEDURE dbo.AddOrUpdateExtendedProperty
@Database NVARCHAR(128) -- Database name
,@Schema NVARCHAR(128) -- Schema name
,@Table NVARCHAR(128) -- Table name
,@Column NVARCHAR(128) -- Column name, NULL if description for table
,@PropertyName NVARCHAR(128) -- Property name
,@PropertyValue SQL_VARIANT -- Property value
AS
BEGIN
SET NOCOUNT ON;
DECLARE @NSQL NVARCHAR(MAX);
DECLARE @Level2Type NVARCHAR(128) = NULL;
DECLARE @Params NVARCHAR(MAX) = N'@Schema NVARCHAR(128), @Table NVARCHAR(128), @Column NVARCHAR(128), @PropertyName NVARCHAR(128), @PropertyValue SQL_VARIANT';
IF @Column IS NOT NULL
BEGIN
SET @NSQL = 'USE ' + @Database + ';
IF NOT EXISTS
(
SELECT NULL FROM sys.extended_properties
WHERE major_id = OBJECT_ID(''' + @Schema + '.' + @Table + ''')
AND name = @PropertyName
AND minor_id = (SELECT column_id
FROM sys.columns
WHERE name = @Column
AND object_id = OBJECT_ID(''' + @Schema + '.' + @Table + ''')
)
)
BEGIN
EXECUTE sp_addextendedproperty
@name = @PropertyName
,@value = @PropertyValue
,@level0type = N''SCHEMA''
,@level0name = @Schema
,@level1type = N''TABLE''
,@level1name = @Table
,@level2type = N''COLUMN''
,@level2name = @Column;
END
ELSE
BEGIN
EXECUTE sp_updateextendedproperty
@name = @PropertyName
,@value = @PropertyValue
,@level0type = N''SCHEMA''
,@level0name = @Schema
,@level1type = N''TABLE''
,@level1name = @Table
,@level2type = N''COLUMN''
,@level2name = @Column;
END
';
EXECUTE sp_executesql
@NSQL
,@Params
,@Schema
,@Table
,@Column
,@PropertyName
,@PropertyValue;
END
ELSE
BEGIN
SET @NSQL = 'USE ' + @Database + ';
IF NOT EXISTS
(
SELECT NULL
FROM sys.extended_properties
WHERE major_id = OBJECT_ID(''' + @Schema + '.' + @Table + ''')
AND name = @PropertyName
AND minor_id = 0
)
BEGIN
EXECUTE sp_addextendedproperty
@name = @PropertyName
,@value = @PropertyValue
,@level0type = N''SCHEMA''
,@level0name = @Schema
,@level1type = N''TABLE''
,@level1name = @Table;
END
ELSE
BEGIN
EXECUTE sp_updateextendedproperty
@name = @PropertyName
,@value = @PropertyValue
,@level0type = N''SCHEMA''
,@level0name = @Schema
,@level1type = N''TABLE''
,@level1name = @Table;
END
';
EXECUTE sp_executesql
@NSQL
,@Params
,@Schema
,@Table
,@Column
,@PropertyName
,@PropertyValue;
END
END
要检查可用于给定表的任何扩展属性,如下所示。
IF EXISTS(SELECT 1 FROM sys.extended_properties WHERE [major_id] = OBJECT_ID('<schema>.<table_name>') AND [name] = N'MS_Description')
如果您的表有多个扩展属性,请将列 ID 指定为minor_id
。
IF EXISTS(SELECT 1 FROM sys.extended_properties WHERE [major_id] = OBJECT_ID('<schema>.<table_name>') AND [name] = N'MS_Description' AND minor_id = 3)
查询sys.extended_properties
目录视图以获取数据库中的所有扩展属性。
有关更多详细信息,请使用http://msdn.microsoft.com/en-us/library/ms177541(v=sql.110).aspx
基于 ScubaSteve 的回答,以下查询将允许您按名称检查指定架构内的列或表上的 MS_Description 属性。要检查表,只需将谓词替换c.name = '<column>'
为c.name IS NULL
或d.minor_id = 0
要按架构枚举 MS_Description 属性,请使用 -
SELECT a.name as [schema], b.name as [table], c.name as [column], d.name, d.value
FROM sys.schemas a
JOIN sys.tables b ON a.schema_id = b.schema_id
LEFT JOIN sys.columns c ON b.object_id = c.object_id
JOIN sys.extended_properties d ON d.major_id = b.object_id AND d.minor_id = ISNULL(c.column_id,0)
WHERE a.name = '<schema>' AND b.name = '<table>' AND c.name = '<column>' AND d.name = 'MS_Description'
要在添加之前检查 MS_Description 属性是否不存在,请使用 -
IF NOT EXISTS (SELECT 1 FROM sys.schemas a JOIN sys.tables b ON a.schema_id = b.schema_id LEFT JOIN sys.columns c ON b.object_id = c.object_id JOIN sys.extended_properties d ON d.major_id = b.object_id AND d.minor_id = ISNULL(c.column_id,0)
WHERE a.name = '<schema>' AND b.name = '<table>' AND c.name = '<column>' AND d.name = 'MS_Description'
)
BEGIN
--EXEC sp_addextendedproperty statement goes here
END
这些查询中的连接可能会更好地排序以消除 ISNULL 函数,但这应该会得到您正在寻找的东西。