2016-11-10 134 views
0

我寫在SQL Server 2016年簡單的存儲過程,但是當我有2條插入語句我不斷收到寫着附近的「終結」存儲過程多個插入錯誤

不正確的語法錯誤

我的語法有問題嗎?或者這是不可能的?

請注意,這是我發送到存儲過程的參數

SQL語句表值參數:

-- This is the stored procedure 
CREATE PROCEDURE [dbo].[SampleProcedure] 
    (-- which accepts 2 table value parameters 
     -- It should be noted that the parameter is readonly 
    @Sample As [dbo].[SampleDataType] Readonly, 
    @Rec As [dbo].[SampleRecType] ReadOnly 
    ) 
AS 
BEGIN 
    -- We simply insert values into the DB table from the parameter 
    -- The table value parameter can be used like a table with only read rights 

    -- INSERT INTO SampleTable(SampleString, SampleDate) 
    --  SELECT SampleString, SampleDate 
    --  FROM @Sample 

    INSERT INTO tbl1Recipients(strEmailSendIDx, strEmailAddress, strDisplayName, strRecipientType) 
    INSERT INTO tbl1SentEmails(strEmailSendID, dtmSent, strSubject, strBody, strConversationID, strConversationTopic, strConversationIndex, dtmReplied, dtmOpened, dtmClicked, blnTrackOpens, blnTrackClicks, blnTrackReplies, lngMergeID, blnHide, lngLatestEventID, strClickResponse, dtmClickResponse) 
     SELECT * 
     FROM @Sample 
END 

編輯,閱讀的意見和建議的答覆這是後解決問題:

-- This is the stored procedure 
CREATE PROCEDURE [dbo].[SampleProcedure] 
    (
    -- which accepts one table value parameter. 
    -- It should be noted that the parameter is readonly 
    @Sample As [dbo].[SampleDataType] Readonly, 
    @Rec As [dbo].[SampleRecType] ReadOnly 
    ) 
AS 
BEGIN 
    -- We simply insert values into the DB table from the parameter 
    -- The table value parameter can be used like a table with only read rights 

    INSERT INTO tbl1Recipients(strEmailSendID, strEmailAddress, strDisplayName, strRecipientType) 
     SELECT * 
     FROM @Rec 

    INSERT INTO tbl1SentEmails(strEmailSendID, dtmSent, strSubject, strBody, strConversationID, strConversationTopic, strConversationIndex, dtmReplied, dtmOpened, dtmClicked, blnTrackOpens, blnTrackClicks, blnTrackReplies, lngMergeID, blnHide, lngLatestEventID, strClickResponse, dtmClickResponse) 
     SELECT * 
     FROM @Sample 
END 

它所需要的是另一種SELECT後,我將數據錄入tbl1Recipients

+0

總之,你的語法是錯誤的。請檢查您的INSERT語句的語法 – Squirrel

+0

@Squirrel如果我刪除'Insert' for'tbl1Recipients'它的工作。我知道多個插入是允許的......但是我檢查了'tbl1Recipients'的值,它似乎沒問題。 –

+0

你的插入語句看起來不對。來自'@sample'的select *的結果不能同時適用於具有4列的表格和具有18列的另一個表格。您應該能夠使用兩個不同的選擇結果集來分隔兩個插入語句,可能會構建'@samplerecipients和'@sampleemails – BasicIsaac

回答

1

這是你的第一個insert

Insert Into tbl1Recipients(strEmailSendIDx, strEmailAddress, strDisplayName, strRecipientType) 
Select * From @Rec 

什麼,預計明年是VALUESSELECT。下一個標記是INSERT,這只是沒有意義。

您需要包含要插入的數據。