我正在尝试在存储过程中使用OUTPUT语句,以便返回新插入行的ID。存储过程是:
CREATE PROCEDURE PROC_RESTORE_REQUEST_TO_QUEUE
@cs_uri_stem varchar(900),
@cs_uri_query varchar(2500),
@date datetime,
@time datetime,
@queue_state smallint,
@process_id int,
@simulation_start_time bigint,
@num_failures smallint
AS
SET NOCOUNT ON
INSERT INTO [DD#WORK].[dbo].[ebhFifoQueue] ([cs-uri-stem],[cs-uri-query],[date],[time],[queue_state],[process_id],[simulation_start_time],[num_failures])
VALUES (@cs_uri_stem,@cs_uri_query,@date,@time,@queue_state,@process_id,@simulation_start_time,@num_failures)
OUTPUT INSERTED.id
当我尝试编译此存储过程时,收到错误消息:
'OUTPUT'附近的语法不正确。
我尝试了几种这种代码的排列无效(相同的错误消息),包括将OUTPUT语句移动到与INSERT语句相同的行。你知道我的语法有什么问题吗?在此先感谢您的帮助,
-Eric
答案 0 :(得分:17)
它的顺序。 The OUTPUT clause应该在INSERT
和VALUES
行之间。
移动你的,就像这样:
INSERT INTO [DD#WORK].[dbo].[ebhFifoQueue] ([cs-uri-stem],[cs-uri-query],[date],[time],[queue_state],[process_id],[simulation_start_time],[num_failures])
OUTPUT INSERTED.id
VALUES (@cs_uri_stem,@cs_uri_query,@date,@time,@queue_state,@process_id,@simulation_start_time,@num_failures)
答案 1 :(得分:2)
我认为应该是这样的:
INSERT INTO [DD#WORK].[dbo].[ebhFifoQueue] ([cs-uri-stem],[cs-uri-query],[date],[time],[queue_state],[process_id],[simulation_start_time],[num_failures])
OUTPUT INSERTED.id
VALUES
(@cs_uri_stem,@cs_uri_query,@date,@time,@queue_state,@process_id,@simulation_start_time,@num_failures)
您还可以在OUPUT语句后添加“INTO @MyVariable”或“INTO MyTable”
答案 2 :(得分:1)
来自MSDN
DECLARE @MyTableVar table( NewScrapReasonID smallint,
Name varchar(50),
ModifiedDate datetime);
INSERT Production.ScrapReason
OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate
INTO @MyTableVar
VALUES (N'Operator error', GETDATE());
答案 3 :(得分:0)
应该是这样的:
CREATE PROCEDURE PROC_RESTORE_REQUEST_TO_QUEUE
@cs_uri_stem varchar(900),
@cs_uri_query varchar(2500),
@date datetime,
@time datetime,
@queue_state smallint,
@process_id int,
@simulation_start_time bigint,
@num_failures smallint,
@new_id int OUTPUT
AS
SET NOCOUNT ON
INSERT INTO [DD#WORK].[dbo].[ebhFifoQueue] ([cs-uri-stem],[cs-uri-query],[date],[time],[queue_state],[process_id],[simulation_start_time],[num_failures])
VALUES (@cs_uri_stem,@cs_uri_query,@date,@time,@queue_state,@process_id,@simulation_start_time,@num_failures)
SET @new_id = SCOPE_IDENTITY()