我正在尝试半自动化创建我的数据库作为其中的一部分,我想添加列描述的扩展属性。当我尝试在我的脚本中运行 sp_sqlexec 时(甚至只是 Exec(@mystring) 我得到一个错误。但是,如果在调试时,我从监视窗口复制动态 sql 字符串,然后在复制的字符串上单独运行 sp_sqlexec窗口我没有得到任何错误并且扩展属性被正确添加。以下脚本演示了该问题:
--Create a table to apply column descriptions to
Create table dbo.table1 (id int, name nvarchar(20));
--Create the table that contains our column descriptions
Create table dbo.column_descs_table (schemaname nvarchar(20), tablename nvarchar(20), columnname nvarchar(20), column_description nvarchar(20))
Insert into column_descs_table (schemaname, tablename, columnname, column_description)
values ('dbo', 'table1', 'id', 'the id column'), ('dbo' , 'table1', 'name', 'the name column');
--Dynamic sql string varaible to hold the commands
Declare @dyn_sql nvarchar(max);
Set @dyn_sql = 'N'''; --Set to opening quote
--now create the string containing commands to add column escriptions
SELECT @dyn_sql = @dyn_sql + N' EXEC sp_addextendedproperty ''''Col Desc'''', ''''' + column_description + N''''', ''''SCHEMA'''', ' + schemaname + N', ''''TABLE'''', ' + tablename + N', ''''COLUMN'''', ' + columnname + N' ;'
FROM dbo.column_descs_table
Set @dyn_sql = @dyn_sql + ''''; --add the closing quote
Print @dyn_sql --If I copy the contents of @dyn_sql here and run seperately it works OK
Exec sp_sqlexec @dyn_sql -- this line causes error
我得到的错误是 Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'EXEC sp_addextendedproperty 'Col Desc', 'the id column', 'SCHEMA', dbo, 'TABLE', table1, 'COLUMN', id ; 执行 sp_addextendedprope'。
但是,如果我单步执行代码并复制 @dyn_sql 的内容,则将其粘贴如下:
exec sp_sqlexec N' EXEC sp_addextendedproperty ''Col Desc'', ''id 列'', ''SCHEMA'', dbo, ''TABLE'', table1, ''COLUMN'', id ; EXEC sp_addextendedproperty ''Col Desc'', ''名称列'', ''SCHEMA'', dbo, ''TABLE'', table1, ''COLUMN'', name ;'
然后上面的工作正常,列描述按预期添加。
非常感谢对此特定复制问题的任何帮助。我确实了解动态 sql 的安全问题(一旦我的设置完成,此脚本将从数据库中删除)
提前致谢
裘德