2010-03-25 26 views
0

INSERT INTO [任務] ([LoginName將] ,[類型] ,[過濾] ,[字典] ,[說明])問題的SQL查詢

Select N'Anonymous',4,'SomeTable.targetcode in (select Code from cities where countrycode in ('TN')) and SomeTable.SomeValue in ('13','15')',3,N'Cities from tunis' 
Union All 
... 

[字典]是一個部分查詢,我需要在我的服務器上運行。

我得到:

Incorrect syntax near ')) and SomeTable.SomeValue in (13,15)'. 

如何修復這個錯誤?

回答

4

這是因爲你在一個值內有撇號。具體來說,您的過濾字符串包括它撇號,這就需要通過他們,加倍進行轉義:

INSERT INTO [Tasks] 
     ([LoginName] 
     ,[Type] 
     ,[Filter] 
     ,[Dictionary] 
     ,[Description]) 

Select N'Anonymous',4,'SomeTable.targetcode in (select Code from cities where countrycode in (''TN'')) and SomeTable.SomeValue in (''13'',''15'')',3,N'Cities from tunis' 
Union All 
1

這是一個有點不清楚正是你正在嘗試做的,表面的價值你想插入的一部分表中的where子句。

因爲在該部分中的引號結束了外部引號,因此引起您注意的是('TN')。嘗試(''TN'')