2012-05-15 45 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的建議。我正在改變我的代碼來使用它(變量作爲參數傳入)。

裘德

相關問題