我工作的地方有一些Excel文件中创建的数据库的数据字典,格式如下:
Database|Instance |Table |Column |Description
BD1 |dbo |Table1 |Column1 |DescriptionA
BD1 |dbo |Table1 |Column2 |DescriptionB
BD1 |dbo |Table1 |Column3 |DescriptionC
BD2 |dbo |Table1 |Column1 |DescriptionD
我需要更新列的描述。我知道我可以逐列使用SSMS,但这是很多工作。 据我所知,我必须更新下表:sys.extended_properties
¿是否有工具或某种方法可以像这样进行主动更新?
答案 0 :(得分:0)
将电子表格导入SQL Server表。 写一个光标循环遍历表,使用:
添加每个属性 Declare @sql as nvarchar(max)
, @Database as nvarchar(max)
, @Instance as nvarchar(100)
, @Table as nvarchar(100)
, @Column as nvarchar(100)
, @Description as nvarchar(100)
Declare mycursor cursor for Select [Database], [Instance], [Table], [Column], [Description] from MyImportedTable
open mycursor
fetch next from mycursor into @Database, @Instance, @Table, @Column, @Description
while @@FETCH_STATUS= 0
begin
set @sql = 'EXEC [' + @Database + '].[' @Schema + '].sp_addextendedproperty
@name = N''Description'',
@value = ''' + @Description + ''',
@level0type = N''Schema'', @level0name = ' + @Instance + ',
@level1type = N''Table'', @level1name = ' + @Table + ',
@level2type = N''Column'', @level2name = ' + @Column + ';'
EXEC (@sql)
fetch next from mycursor into @Database, @Instance, @Table, @Column, @Description
END
close mycursor
deallocate mycursor