2017-05-12 70 views
1

我想插入一條記錄,然後用存儲過程返回該記錄的細節:嵌套的INSERT,UPDATE,DELETE或MERGE語句在SELECT語句是不允許

USE 
TEST 
GO 

CREATE PROCEDURE AddProject(
    -- In 
    @_title NVARCHAR(200), 
    @_description NVARCHAR(MAX), 
    -- Out 
    @Title NVARCHAR(200) OUT, 
    @Description NVARCHAR(MAX) OUT) 
AS 
BEGIN 
    SELECT 
     [ProjectsTable.ProjectID], 
     [Title], 
     [Description] 
    FROM 
     (INSERT INTO [Projects] ("Title", "Description") 
     OUTPUT inserted.ProjectID 
     VALUES (@_title, @_description)) ProjectsTable 
END 

我米得到這個錯誤,我做錯了什麼?

消息10729,級別15,狀態1,過程AddProject,第31行
嵌套INSERT,UPDATE,DELETE或MERGE語句未SELECT語句不是行的直接來源用於允許INSERT語句。

感謝回答:


的程序弄成這個樣子:

USE 
    TEST 
GO 
    CREATE PROCEDURE AddProject( 

     -- In  
     @_title NVARCHAR(200), 
     @_description NVARCHAR(MAX),  
     -- Out  
     @ProjectID INT OUT, 
     @Title NVARCHAR(200) OUT, 
     @Description NVARCHAR(MAX) OUT 

    ) 
    AS 
    BEGIN  

     BEGIN TRAN 

      INSERT INTO  
       [Projects]  
        ("Title", "Description") 
       VALUES  
        (@_title, @_description)     

      SET @ProjectID = SCOPE_IDENTITY(); 

      SELECT 
       @Title = [Title], 
       @Description = [Description]     
      FROM 
       [Projects] 
      WHERE 
       [Projects].[ProjectID] = @ProjectID   

     COMMIT 

    END 

我打電話這樣說:

Declare @ProjectID as INT 
Declare @Title as NVARCHAR(200) 
Declare @Description as NVARCHAR(MAX) 

EXEC AddProject "Test project", "A test project", @ProjectID output, @Title output, @Description output 

SELECT @ProjectID, @Title, @Description 
+0

什麼是迴歸了,你正在傳遞同一數據的目的是什麼? –

+0

首先插入,然後獲取ID的SCOPE_IDENTITY(),然後返回Select語句。但我同意@ George2.0Hope。 –

+0

@ George2.0Hope - 注意來自INSERT的輸出 – Jimmyt1988

回答

3

只取最新插入的Ide從你的表中恢復標題和描述。

CREATE PROCEDURE AddProject(
    -- In 
    @_title NVARCHAR(200), 
    @_description NVARCHAR(MAX), 
    -- Out 
    @Title NVARCHAR(200) OUT, 
    @Description NVARCHAR(MAX) OUT, 
    @ProjectID INT OUT) 
AS 
BEGIN 
    BEGIN TRAN 
     INSERT INTO [Projects] (@_title, @_description); 
     SET @ProjectID = SCOPE_INDENTITY(); 
     SELECT @Title = [Title], @Description = [Description] FROM [Projects] WHERE [Projects.ProjectID] = @ProjectID; 
    COMMIT 
END 

公告實施新專案編號OUT參數

SCOPE_IDENTITY

+0

哈,好點,我錯過了那部分。怎麼樣使用OUTPUT關鍵字...這個問題是我可能需要的一個例子,但我可能需要使用INSERT的輸出,因爲我可能有多個插入 – Jimmyt1988

+0

您可以根據需要多次重複該事務,SCOPE_IDENTITY會在當前的環境中爲您提供最新創建的身份。我會把文件鏈接到它(檢查出來) – Hybris95

+0

謝謝你的傢伙。備查。你知道如何使用OUTPUT關鍵字查詢嗎? – Jimmyt1988

0
CREATE PROCEDURE AddProject(
    -- In 
    @_title NVARCHAR(200), 
    @_description NVARCHAR(MAX), 
    -- Out 
    @Title NVARCHAR(200) OUT, 
    @Description NVARCHAR(MAX) OUT, 
    @ProjectID INT OUT, 
    @err INT OUT) 
AS 
BEGIN 
    BEGIN TRAN 
     INSERT INTO [Projects] (@_title, @_description); 
     SET @ProjectID = SCOPE_INDENTITY(); 
     SET @Title = (SELECT [Title] FROM [Projects] WHERE [Projects.ProjectID] = @ProjectID); 
     SET @Description = (SELECT [Description] FROM [Projects] WHERE [Projects.ProjectID] = @ProjectID); 
     SELECT @err = @@ERROR IF @err <> 0 BEGIN ROLLBACK TRANSACTION RETURN @err END 
    COMMIT 
END 
+0

我建議在發生錯誤時使用TRY CATCH塊 – Hybris95

+0

@ Hybris95我想這取決於您是否想在DBA級別或代碼級別處理錯誤,是的? –

+0

無論如何,如果發生錯誤,考慮到它只能是INSERT,它會自動ROLLBACKed,讓錯誤發生並在應用程序級別管理 – Hybris95