使用临时 table 扩展 属性 批量更新

extended property mass update using temporary table

我需要半自动化扩展 属性 更新,我计划做的事情,无论对错,都是用所有数据填充临时 table,然后以某种方式加入到检查值是否已存在时的临时 table。 然后我想遍历临时 table 中的每一行,如果该值存在,则调用函数 sp_updateextendedproperty,如果不存在,则调用函数 sp_addextendedproperty。以下是我的出发点,是什么让这项工作成功?

DECLARE @Table TABLE (id int IDENTITY(1,1),TableName SYSNAME, ColumnName varchar(200), ColumnDescription varchar(200))

INSERT INTO @Table VALUES('MyTable', 'Col1', 'Col1 description')
INSERT INTO @Table VALUES('MyTable', 'Col2', 'Col2 description')

IF EXISTS 
(select 
    sc.name,
    sep.value
from sys.tables st
inner join sys.columns sc on st.object_id = sc.object_id
left join sys.extended_properties sep on st.object_id = sep.major_id
                                 and sc.column_id = sep.minor_id
                                 and sep.name = 'MS_Description'
where st.name = @Table.TableName and sc.name = @Table.ColumnName and sep.value is not null)

EXEC sp_updateextendedproperty 
@name = N'MS_Description', @value = @Table.ColumnDescription,
@level0type = N'Schema', @level0name = 'dbo', 
@level1type = N'Table',  @level1name = @Table.TableName, 
@level2type = N'Column', @level2name = @Table.ColumnName

ELSE

EXEC sp_addextendedproperty 
@name = N'MS_Description', @value = @Table.ColumnDescription,
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = @Table.TableName, 
@level2type = N'Column', @level2name = @Table.ColumnName

新脚本

CREATE TABLE updateTable (TableName SYSNAME, ColumnName varchar(100), ColumnDescription varchar(100))

INSERT INTO updateTable VALUES('tblHAEMATOLOGY_MDT', 'MDT_ID', 'row1 test run')
INSERT INTO updateTable VALUES('tblHAEMATOLOGY_MDT', 'MEETING_ID', 'row2 test run')

DECLARE @TableName SYSNAME, @ColumnName varchar(100), @ColumnDescription varchar(100)
DECLARE @UpdateCursor CURSOR

SET @UpdateCursor = CURSOR FOR
SELECT TableName, ColumnName, ColumnDescription FROM updateTable

OPEN @UpdateCursor

FETCH NEXT FROM @UpdateCursor INTO @TableName, @ColumnName, @ColumnDescription

WHILE @@FETCH_STATUS = 0
BEGIN

IF EXISTS 
(select 
    sc.name,
    sep.value
from sys.tables st
inner join sys.columns sc on st.object_id = sc.object_id
left join sys.extended_properties sep on st.object_id = sep.major_id
                                 and sc.column_id = sep.minor_id
                                 and sep.name = 'MS_Description'
left join updateTable on st.name = @TableName and sc.name = @ColumnName
where sep.value is not null)

BEGIN

EXEC sp_updateextendedproperty 
@name = N'MS_Description', @value = @ColumnDescription,
@level0type = N'Schema', @level0name = 'dbo', 
@level1type = N'Table',  @level1name = @TableName, 
@level2type = N'Column', @level2name = @ColumnName

FETCH NEXT FROM @UpdateCursor INTO @TableName, @ColumnName, @ColumnDescription

END

ELSE

BEGIN

EXEC sp_addextendedproperty 
    @name = N'MS_Description', @value = @ColumnDescription,
    @level0type = N'Schema', @level0name = 'dbo',
    @level1type = N'Table', @level1name = @TableName, 
    @level2type = N'Column', @level2name = @ColumnName

FETCH NEXT FROM @UpdateCursor INTO @TableName, @ColumnName, @ColumnDescription

END

END

CLOSE @UpdateCursor
DEALLOCATE @UpdateCursor

这是我极少数会这样说的次数之一,但解决方案是使用游标。只需针对您的 table 变量和 运行 您在循环内的现有查询创建一个游标循环。我以前做过这个完全相同的逻辑,它应该可以正常工作。