Return value in SQL Server stored procedure

前端 未结 4 874
忘掉有多难
忘掉有多难 2021-02-05 12:36

I have a stored procedure that has an if statement in it. If the number of rows counted is greater than 0 then it should set the only output parameter @UserId to 0<

4条回答
  •  长情又很酷
    2021-02-05 13:14

    I can recommend make pre-init of future index value, this is very usefull in a lot of case like multi work, some export e.t.c.

    just create additional User_Seq table: with two fields: id Uniq index and SeqVal nvarchar(1)

    and create next SP, and generated ID value from this SP and put to new User row!

    CREATE procedure [dbo].[User_NextValue]
    as
    begin
        set NOCOUNT ON
    
    
        declare @existingId int = (select isnull(max(UserId)+1, 0)  from dbo.User)
    
        insert into User_Seq (SeqVal) values ('a')
        declare @NewSeqValue int = scope_identity()     
    
        if @existingId > @NewSeqValue 
        begin  
    
            set identity_insert User_Seq  on
            insert into User_Seq (SeqID) values (@existingId)     
            set @NewSeqValue = scope_identity()     
        end
    
        delete from User_Seq WITH (READPAST)
    
    return @NewSeqValue
    
    end
    

提交回复
热议问题