sqlsql-serversql-server-2008relational-databaseextended-properties

extended property mass update using temporary table


I need to semi-automate an extended property update and what I planned to do, rightly or wrongly, is to populate a temporary table with all of the data, then somehow join to the temporary table when checking to see if the value already exists. Then I want to iterate through each row in the temporary table and if the value exists, call the function sp_updateextendedproperty, and if it doesn't call the function sp_addextendedproperty. Below is my starting point, what would make this work?

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

New script

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

Solution

  • This is one of the very few times that I would say this, but the solution is to use a cursor. Just create a cursor loop against your table variable and run your exists query inside the loop. I've done this very exact same logic before and it should work fine.