views:

46

answers:

4

I'm trying to use the OUTPUT statement in a stored procedure in order to return the ID of a newly inserted row. The stored procedure is:

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

When I try to compile this stored procedure, I get an error message:

Incorrect syntax near 'OUTPUT'.


I've tried several permutations of this code to no avail (same error message), including moving the OUTPUT statement onto the same line as the INSERT statement. Do you know what the problem is with my syntax? Thanks in advance for your help,

-Eric

+1  A: 

From 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());
Noel Abrahams
+4  A: 

Its the order. The OUTPUT clause should go between the INSERT and the VALUES lines.

Just move yours, like this:

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) 
Gabriel McAdams
Thanks, this works!
Eric Hendrickson
+2  A: 

I think it should be like:

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)

You can also add a "INTO @MyVariable" or "INTO MyTable" after the OUPUT statement

Antonio
If you want to use the output variable later it is best to output it to a table variable (if you would expect multiple rows to be inserted/updated/deleted) or a varaible if you are using a syntax that will guarantee only one record.
HLGEM
yes, I meant a table variable...
Antonio
A: 

Should be something like:

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()
Russell McClure