Correct use of SCOPE_IDENTITY function within simple stored procedure

后端 未结 3 1819
野性不改
野性不改 2021-02-10 11:20

I\'d like to simply send some information from a simple client to a log file and then use the identity created for further processing.

Is the following use of SCOP

3条回答
  •  花落未央
    2021-02-10 11:48

    Seems like this is the best approach - can see a few references advising to only use RETURN as a way of communicating state or errors so an OUTPUT parameter is better practice:

    ALTER PROCEDURE [dbo].[LogSearch]
        @userName      VARCHAR(50),
        @dateTimeStart DATETIME,
        @searchID      INT OUTPUT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        INSERT INTO [WH].[dbo].[tb_Searches]
                    (
                    UserName,
                    DateTimeStart
                    )
        VALUES  
                    (
                    @userName, 
                    @dateTimeStart
                    );
    
        SET @searchID = SCOPE_IDENTITY();
    
    END;
    

提交回复
热议问题