2012-05-15 41 views
0

我试图半自动创建我的数据库 作为这一部分,我想添加列描述的扩展属性。 当我尝试在脚本中运行sp_sqlexec(或者甚至只是Exec(@mystring)时,出现错误,但是如果在调试时从监视窗口复制动态sql字符串,然后在复制的字符串上运行sp_sqlexec单独的窗口我没有得到任何错误和扩展属性正确添加 下面的脚本演示该问题:在SSMS复制的字符串与原始字符串有不同的行为

--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 

我得到的错误是 消息102,15级,状态1,行 附近有语法错误'EXEC sp_addextendedproperty '山口说明', 'id列', 'SCHEMA',DBO, 'TABLE',表1, '列',ID; EXEC 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'',名称;'

然后,上述工作正常,并按预期添加列描述。

对这个特定的复制问题的任何帮助非常感谢。我明白使用动态SQL的安全问题(这个脚本会从数据库中删除一次我的设置完成)提前

感谢

裘德

+0

sp_sqlexec在SQL Server 7假想删除,不再正式支持(http://technet.microsoft.com/en-us/library/cc917540.aspx)。我很惊讶它仍然存在于SQL2008R2中 - 在线文档早已不复存在。 – MartW

+0

感谢您的建议。我将更改脚本以使用sp_executesql。 –

回答

0

看起来它是因为你领先N被列入要执行的字符串;你根本不需要它。换句话说,你最后得到了这样的事情:

exec sp_execsql 'N'' exec sp_addextendedproperty /* etc. */ '''

但它应该是这样的:

exec sp_execsql N'exec sp_addextendedproperty /* etc. */ '

但是你为什么即使在使用动态SQL吗?所有传递给sp_addextendedproperty的值都可以是passed as parameters,所以没有明显的理由使用动态SQL,除非你已经简化了一些问题。

最后,您应该使用sp_executesql,它是preferred way来执行动态SQL。

+0

谢谢。我现在尝试删除N并将sp_sqlexec更改为sp_executesql。可悲的是我仍然遇到同样的问题。有关更改为sp_executesql的建议非常有用,尽管我会将原始脚本更改为使用此脚本(此处的脚本是其简化版本),并查看是否可以摆脱一些动态sql。感谢您的帮助。 –

+0

是的,您应该使用sp_executesql,因为您可以将参数传递给它,以便删除引用和构建字符串的任何问题。 – Pondlife

+0

我已经解决了我的字符串复制问题。 SQL删除了一些它认为是过剩的双引号。这是一个显示问题和解决方案的简单示例: –

0

我相信我已经解决了我的字符串复制问题。 SQL在连接字符串中检测到双引号作为空字符串并将其删除。一个简单的例子显示的问题和我的解决方案如下:

--Example to Select 'simple string' and then 'concat string' into results sets 
DECLARE 
    @Simplestring nvarchar(max) = '' , 
    @Concatstring nvarchar(max) = '' , 
    @Stringvar nvarchar(10) = 'string'; 

--The double quotes in next line are the quotemark we want plus a quotemark acting 
--as an escape character 
[email protected] will be set to 'Select 'simple string' ' 
SET @Simplestring = 'Select ''simple string'' '; 

--Similarly we need @concatstring to be set to 'Select 'Concat string' ' 
SET @Concatstring = 'Select '' concat' + @Stringvar + ''; -- this wont work the last 
--double quote will be removed 

--Add a character that cannot appear in any othe part of the concatenation - I've used * 
SET @Concatstring = 'Select '' Concat ' + @Stringvar + '*'; 
--Now replace the * with a quote mark 
SET @Concatstring = REPLACE(@Concatstring , '*' , ''''); -- This will work 

EXEC sp_executesql @Simplestring; 
EXEC sp_executesql @Concatstring; 

可能比我的简单的解决方案。

非常感谢您对使用sp_executesql的建议。我正在改变我的代码来使用它(变量作为参数传入)。

裘德

相关问题