問題:','附近的語法不正確。 (程序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 '='
你值列表中包含不屬於SQL字符串的一部分逗號(,他們沒有適當的引用) – JohnHC
代替'EXEC(@sql)''按SELECT(@sql)'或'打印@ sql' – McNets
唯一動態的是你的表名,表明你「做錯了」。你已經創建了動態表(具有相同的列),而不是一個正確的數據庫結構。 – Jamiec