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

时间:2016-03-18 15:46:31

标签: sql sql-server sql-server-2008 relational-database extended-properties

我需要对扩展属性更新进行半自动化,无论是对还是错,我计划做的是用所有数据填充临时表,然后在检查是否值时以某种方式连接到临时表已经存在。 然后我想遍历临时表中的每一行,如果值存在,则调用函数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

1 个答案:

答案 0 :(得分:1)

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