我想插入一條記錄,然後用存儲過程返回該記錄的細節:嵌套的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
什麼是迴歸了,你正在傳遞同一數據的目的是什麼? –
首先插入,然後獲取ID的SCOPE_IDENTITY(),然後返回Select語句。但我同意@ George2.0Hope。 –
@ George2.0Hope - 注意來自INSERT的輸出 – Jimmyt1988