2017-02-22 53 views
0

問題:','附近的語法不正確。 (程序spAddNewDataToHoldingTable,71號線)「,」附近的語法不正確。當使用Exec時

目的:插入數據表(這是我通過其他存儲過程創建)

思考:我已經試過撇號的幾個組合,如果我得到的SQL語句運行則意味着在第一個值之前沒有撇號,因此出現錯誤。

我希望在錯誤的地方有一個撇號就是它的全部。

如果我沒有變量名稱,我可以在沒有EXEC的情況下運行,但是我也希望使其變得安全。

代碼:

USE [DB] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[spAddNewDataToHoldingTable] 

@sTableName varchar(50), 
@sPMNUM varchar(8), 
@sUPRN varchar(510) , 
@sSurveyDate Date, 
@sVFMDISCIPLINE varchar(4) , 
@sVFMDISCIPLINEELEMENT varchar(510) , 
@sWorkOrder varchar(510) , 
@sNextSurveyDue Date , 
@sRiskCategory varchar(510) , 
@sDocURL varchar(MAX) , 
@sDocLinks varchar(MAX) , 
@sVINCIRISKLevel varchar(510) , 
@sRemedialActionWO varchar(510) , 
@sRemedialActionDate date, 
@sComments varchar(MAX) , 
@sRISKNUM varchar(510) , 
@sRISKOWNER varchar(510) , 
@sSurveyorsComments varchar(MAX) , 
@sRecommendations varchar(510) , 
@sStatusOfIssue int , 
@sClientContract varchar(12) , 
@sJPNUM varchar(12) 


AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
SET NOCOUNT ON; 

DECLARE @sTablename_ AS varchar(max) 

SET @sTablename_ = LTRIM(RTRIM(@sTableName)) 

DECLARE @sql NVARCHAR(2500) 

SET @sql = ' 
INSERT INTO ' + @sTablename_ + ' 
(
PMNUM, 
UPRN, 
VFMDISPLINE, 
VFMDISPLINEELEMENT, 
WorkOrder, 
RiskCategory, 
DocURL, 
DocLinks, 
VINCIRISKLevel, 
RemedialActionWO, 
Comments, 
RISKNUM, 
RISKOWNER, 
SurveyorsComments, 
Recommendation, 
StatusOfIssue, 
ClientContract, 
JPNUM, 
RemedialActionDate , 
SurveyDate , 
NextSurveyDate 
) 

VALUES 

(''' 

+ ''' + QUOTENAME(@sPMNUM) + ''' 
, ''' + QUOTENAME(@sUPRN) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINE) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINEELEMENT) + ''' 
, ''' + QUOTENAME(@sWorkOrder) + ''' 
, ''' + QUOTENAME(@sRiskCategory) + ''' 
, ''' + QUOTENAME(@sDocURL) + ''' 
, ''' + QUOTENAME(@sDocLinks) + ''' 
, ''' + QUOTENAME(@sVINCIRISKLevel) + ''' 
, ''' + QUOTENAME(@sRemedialActionWO) + ''' 
, ''' + QUOTENAME(@sComments) + ''' 
, ''' + QUOTENAME(@sRISKNUM) + ''' 
, ''' + QUOTENAME(@sRISKOWNER) + ''' 
, ''' + QUOTENAME(@sSurveyorsComments) + ''' 
, ''' + QUOTENAME(@sRecommendations) + ''' 
, ''' + QUOTENAME(@sStatusOfIssue + ''' 
, ''' + QUOTENAME(@sClientContract) + ''' 
, ''' + QUOTENAME(@sJPNUM) + ''' 
, ''' + QUOTENAME(@sRemedialActionDate) + ''' 
, ''' + QUOTENAME(@sSurveyDate) + ''' 
, ''' + QUOTENAME(@sNextSurveyDue) + ''' 
+ ''')''' 

EXEC (@sql) 

END 

編號:

SQL Server incorrect syntax near ')' Incorrect syntax near '(' and near '='

+0

你值列表中包含不屬於SQL字符串的一部分逗號(,他們沒有適當的引用) – JohnHC

+3

代替'EXEC(@sql)''按SELECT(@sql)'或'打印@ sql' – McNets

+1

唯一動態的是你的表名,表明你「做錯了」。你已經創建了動態表(具有相同的列),而不是一個正確的數據庫結構。 – Jamiec

回答

0

你加入另一個 ''」

這裏更新一個

USE [DB] 
GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER PROCEDURE [dbo].[spAddNewDataToHoldingTable] 

@sTableName varchar(50), 
@sPMNUM varchar(8), 
@sUPRN varchar(510) , 
@sSurveyDate Date, 
@sVFMDISCIPLINE varchar(4) , 
@sVFMDISCIPLINEELEMENT varchar(510) , 
@sWorkOrder varchar(510) , 
@sNextSurveyDue Date , 
@sRiskCategory varchar(510) , 
@sDocURL varchar(MAX) , 
@sDocLinks varchar(MAX) , 
@sVINCIRISKLevel varchar(510) , 
@sRemedialActionWO varchar(510) , 
@sRemedialActionDate date, 
@sComments varchar(MAX) , 
@sRISKNUM varchar(510) , 
@sRISKOWNER varchar(510) , 
@sSurveyorsComments varchar(MAX) , 
@sRecommendations varchar(510) , 
@sStatusOfIssue int , 
@sClientContract varchar(12) , 
@sJPNUM varchar(12) 


AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
SET NOCOUNT ON; 

DECLARE @sTablename_ AS varchar(max) 

SET @sTablename_ = LTRIM(RTRIM(@sTableName)) 

DECLARE @sql NVARCHAR(2500) 

SET @sql = ' 
INSERT INTO ' + @sTablename_ + ' 
(
PMNUM, 
UPRN, 
VFMDISPLINE, 
VFMDISPLINEELEMENT, 
WorkOrder, 
RiskCategory, 
DocURL, 
DocLinks, 
VINCIRISKLevel, 
RemedialActionWO, 
Comments, 
RISKNUM, 
RISKOWNER, 
SurveyorsComments, 
Recommendation, 
StatusOfIssue, 
ClientContract, 
JPNUM, 
RemedialActionDate , 
SurveyDate , 
NextSurveyDate 
) 

VALUES 

(''' + QUOTENAME(@sPMNUM) + ''' 
, ''' + QUOTENAME(@sUPRN) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINE) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINEELEMENT) + ''' 
, ''' + QUOTENAME(@sWorkOrder) + ''' 
, ''' + QUOTENAME(@sRiskCategory) + ''' 
, ''' + QUOTENAME(@sDocURL) + ''' 
, ''' + QUOTENAME(@sDocLinks) + ''' 
, ''' + QUOTENAME(@sVINCIRISKLevel) + ''' 
, ''' + QUOTENAME(@sRemedialActionWO) + ''' 
, ''' + QUOTENAME(@sComments) + ''' 
, ''' + QUOTENAME(@sRISKNUM) + ''' 
, ''' + QUOTENAME(@sRISKOWNER) + ''' 
, ''' + QUOTENAME(@sSurveyorsComments) + ''' 
, ''' + QUOTENAME(@sRecommendations) + ''' 
, ''' + QUOTENAME(@sStatusOfIssue) + ''' 
, ''' + QUOTENAME(@sClientContract) + ''' 
, ''' + QUOTENAME(@sJPNUM) + ''' 
, ''' + QUOTENAME(@sRemedialActionDate) + ''' 
, ''' + QUOTENAME(@sSurveyDate) + ''' 
, ''' + QUOTENAME(@sNextSurveyDue) + ''')' 

EXEC (@sql) 

END 
+1

檢查結束''' - 我認爲也有太多報價 –

+0

是的,謝謝,更新 –

1

對於所有這些,您不需要使用quotename(),它只是將它們包裝在方括號中。

別忘了convert()您的date數據類型爲varchar

ALTER PROCEDURE [dbo].[spAddNewDataToHoldingTable] 

@sTableName varchar(50), 
@sPMNUM varchar(8), 
@sUPRN varchar(510) , 
@sSurveyDate Date, 
@sVFMDISCIPLINE varchar(4) , 
@sVFMDISCIPLINEELEMENT varchar(510) , 
@sWorkOrder varchar(510) , 
@sNextSurveyDue Date , 
@sRiskCategory varchar(510) , 
@sDocURL varchar(MAX) , 
@sDocLinks varchar(MAX) , 
@sVINCIRISKLevel varchar(510) , 
@sRemedialActionWO varchar(510) , 
@sRemedialActionDate date, 
@sComments varchar(MAX) , 
@sRISKNUM varchar(510) , 
@sRISKOWNER varchar(510) , 
@sSurveyorsComments varchar(MAX) , 
@sRecommendations varchar(510) , 
@sStatusOfIssue int , 
@sClientContract varchar(12) , 
@sJPNUM varchar(12) 


AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
SET NOCOUNT ON; 

DECLARE @sTablename_ AS varchar(max) 

SET @sTablename_ = LTRIM(RTRIM(@sTableName)) 

DECLARE @sql NVARCHAR(2500) 

SET @sql = ' 
INSERT INTO ' + @sTablename_ + ' 
(
PMNUM, 
UPRN, 
VFMDISPLINE, 
VFMDISPLINEELEMENT, 
WorkOrder, 
RiskCategory, 
DocURL, 
DocLinks, 
VINCIRISKLevel, 
RemedialActionWO, 
Comments, 
RISKNUM, 
RISKOWNER, 
SurveyorsComments, 
Recommendation, 
StatusOfIssue, 
ClientContract, 
JPNUM, 
RemedialActionDate , 
SurveyDate , 
NextSurveyDate 
) 

VALUES 

(' 

+ '''' + @sPMNUM    + '''' 
+ ', ''' + @sUPRN     + '''' 
+ ', ''' + @sVFMDISCIPLINE  + '''' 
+ ', ''' + @sVFMDISCIPLINEELEMENT + '''' 
+ ', ''' + @sWorkOrder   + '''' 
+ ', ''' + @sRiskCategory   + '''' 
+ ', ''' + @sDocURL    + '''' 
+ ', ''' + @sDocLinks    + '''' 
+ ', ''' + @sVINCIRISKLevel  + '''' 
+ ', ''' + @sRemedialActionWO  + '''' 
+ ', ''' + @sComments    + '''' 
+ ', ''' + @sRISKNUM    + '''' 
+ ', ''' + @sRISKOWNER   + '''' 
+ ', ''' + @sSurveyorsComments + '''' 
+ ', ''' + @sRecommendations  + '''' 
+ ', ''' + @sStatusOfIssue  + '''' 
+ ', ''' + @sClientContract  + '''' 
+ ', ''' + @sJPNUM    + '''' 
+ ', ''' + convert(varchar(10),@sRemedialActionDate,120)+ '''' 
+ ', ''' + convert(varchar(10),@sSurveyDate,120)  + '''' 
+ ', ''' + convert(varchar(10),@sNextSurveyDue,120)  + '''' 
+ ');' 

EXEC (@sql) 
END; 
0

爲了避免單引號,您需要將其加倍。 因此「」「‘’」將返回「」

所以,你的代碼應該是這樣的:

SET @sql = ' 
INSERT INTO ' + @sTablename_ + ' 
(
PMNUM, 
UPRN, 
VFMDISPLINE, 
VFMDISPLINEELEMENT, 
WorkOrder, 
RiskCategory, 
DocURL, 
DocLinks, 
VINCIRISKLevel, 
RemedialActionWO, 
Comments, 
RISKNUM, 
RISKOWNER, 
SurveyorsComments, 
Recommendation, 
StatusOfIssue, 
ClientContract, 
JPNUM, 
RemedialActionDate , 
SurveyDate , 
NextSurveyDate 
) 

VALUES 

(
''' + QUOTENAME(@sPMNUM) + ''' 
, ''' + QUOTENAME(@sUPRN) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINE) + ''' 
, ''' + QUOTENAME(@sVFMDISCIPLINEELEMENT) + ''' 
, ''' + QUOTENAME(@sWorkOrder) + ''' 
, ''' + QUOTENAME(@sRiskCategory) + ''' 
, ''' + QUOTENAME(@sDocURL) + ''' 
, ''' + QUOTENAME(@sDocLinks) + ''' 
, ''' + QUOTENAME(@sVINCIRISKLevel) + ''' 
, ''' + QUOTENAME(@sRemedialActionWO) + ''' 
, ''' + QUOTENAME(@sComments) + ''' 
, ''' + QUOTENAME(@sRISKNUM) + ''' 
, ''' + QUOTENAME(@sRISKOWNER) + ''' 
, ''' + QUOTENAME(@sSurveyorsComments) + ''' 
, ''' + QUOTENAME(@sRecommendations) + ''' 
, ''' + QUOTENAME(@sStatusOfIssue) + ''' 
, ''' + QUOTENAME(@sClientContract) + ''' 
, ''' + QUOTENAME(@sJPNUM) + ''' 
, ''' + QUOTENAME(@sRemedialActionDate) + ''' 
, ''' + QUOTENAME(@sSurveyDate) + ''' 
, ''' + QUOTENAME(@sNextSurveyDue) + ''')' 
+0

爲什麼我的回答被拒絕?它解決了OP在給定查詢中錯誤語法的問題。 –

0

我不認爲你應該在這裏使用動態SQL,你而應該修復設計,導致您必須使用完全相同的模式才能使用多個表。但是,如果你要堅持這一點,你需要在你的動態sql中使用參數,而不是試圖在整個地方打單引號。簡單地添加一組雙引號只會延遲這個問題。以下是您可以如何使用動態sql執行此操作並參數化所有值。

ALTER PROCEDURE [dbo].[spAddNewDataToHoldingTable] 
(
    @sTableName varchar(50), 
    @sPMNUM varchar(8), 
    @sUPRN varchar(510) , 
    @sSurveyDate Date, 
    @sVFMDISCIPLINE varchar(4) , 
    @sVFMDISCIPLINEELEMENT varchar(510) , 
    @sWorkOrder varchar(510) , 
    @sNextSurveyDue Date , 
    @sRiskCategory varchar(510) , 
    @sDocURL varchar(MAX) , 
    @sDocLinks varchar(MAX) , 
    @sVINCIRISKLevel varchar(510) , 
    @sRemedialActionWO varchar(510) , 
    @sRemedialActionDate date, 
    @sComments varchar(MAX) , 
    @sRISKNUM varchar(510) , 
    @sRISKOWNER varchar(510) , 
    @sSurveyorsComments varchar(MAX) , 
    @sRecommendations varchar(510) , 
    @sStatusOfIssue int , 
    @sClientContract varchar(12) , 
    @sJPNUM varchar(12) 
) AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    DECLARE @sql NVARCHAR(max) 

    SET @sql = N' 
    INSERT INTO ' + QUOTENAME(LTRIM(RTRIM(@sTableName))) + ' 
    (
     PMNUM 
     , UPRN 
     , VFMDISPLINE 
     , VFMDISPLINEELEMENT 
     , WorkOrder 
     , RiskCategory 
     , DocURL 
     , DocLinks 
     , VINCIRISKLevel 
     , RemedialActionWO 
     , Comments 
     , RISKNUM 
     , RISKOWNER 
     , SurveyorsComments 
     , Recommendation 
     , StatusOfIssue 
     , ClientContract 
     , JPNUM 
     , RemedialActionDate 
     , SurveyDate 
     , NextSurveyDate 
    ) 
    VALUES 
    (
     @sPMNUM 
     , @sUPRN 
     , @sVFMDISCIPLINE 
     , @sVFMDISCIPLINEELEMENT 
     , @sWorkOrder 
     , @sRiskCategory 
     , @sDocURL 
     , @sDocLinks 
     , @sVINCIRISKLevel 
     , @sRemedialActionWO 
     , @sComments 
     , @sRISKNUM 
     , @sRISKOWNER 
     , @sSurveyorsComments 
     , @sRecommendations 
     , @sStatusOfIssue 
     , @sClientContract 
     , @sJPNUM 
     , @sRemedialActionDate 
     , @sSurveyDate 
     , @sNextSurveyDue 
    )' 

    EXEC sp_executesql @sql 
     , N'@sPMNUM varchar(8) 
      , @sUPRN varchar(510) 
      , @sVFMDISCIPLINE varchar(4) 
      , @sVFMDISCIPLINEELEMENT varchar(510) 
      , @sWorkOrder varchar(510) 
      , @sRiskCategory varchar(510) 
      , @sDocURL varchar(MAX) 
      , @sDocLinks varchar(MAX) 
      , @sVINCIRISKLevel varchar(510) 
      , @sRemedialActionWO varchar(510) 
      , @sComments varchar(MAX) 
      , @sRISKNUM varchar(510) 
      , @sRISKOWNER varchar(510) 
      , @sSurveyorsComments varchar(MAX) 
      , @sRecommendations varchar(510) 
      , @sStatusOfIssue int 
      , @sClientContract varchar(12) 
      , @sJPNUM varchar(12) 
      , @sRemedialActionDate varchar(10) 
      , @sSurveyDate varchar(10) 
      , @sNextSurveyDue varchar(10)' 
     , @sPMNUM = @sPMNUM 
     , @sUPRN = @sUPRN 
     , @sVFMDISCIPLINE = @sVFMDISCIPLINE 
     , @sVFMDISCIPLINEELEMENT = @sVFMDISCIPLINEELEMENT 
     , @sWorkOrder = @sWorkOrder 
     , @sRiskCategory = @sRiskCategory 
     , @sDocURL = @sDocURL 
     , @sDocLinks = @sDocLinks 
     , @sVINCIRISKLevel = @sVINCIRISKLevel 
     , @sRemedialActionWO = @sRemedialActionWO 
     , @sComments = @sComments 
     , @sRISKNUM = @sRISKNUM 
     , @sRISKOWNER = @sRISKOWNER 
     , @sSurveyorsComments = @sSurveyorsComments 
     , @sRecommendations = @sRecommendations 
     , @sStatusOfIssue = @sStatusOfIssue 
     , @sClientContract = @sClientContract 
     , @sJPNUM = @sJPNUM 
     , @sRemedialActionDate = @sRemedialActionDate 
     , @sSurveyDate = @sSurveyDate 
     , @sNextSurveyDue = @sNextSurveyDue  
END;