Is it possible in sql server using stored procedure to return the identity column value in a table against which some values are inserted? For example using stored procedure if we insert data in a table:
Table TBL
So if I run the store procedure inserting some values like:
Insert into TBL (Name, UserName, Password) Values ('example', 'example', '$2a$12$00WFrz3dOfLaIgpTYRJ9CeuB6VicjLGhLset8WtFrzvtpRekcP1lq')
How can I return the value of UserID
at which this insertion will take place. I need The value of UserID for some other operations, can anybody solve this?
Once we insert a row in a table, the @@IDENTITY function column gives the IDENTITY value generated by the statement. If we run any query that did not generate IDENTITY values, we get NULL value in the output. The SQL @@IDENTITY runs under the scope of the current session.
SCOPE_IDENTITY() returns the last identity value generated for any table in the current session and the current scope. Generally what you want to use. IDENT_CURRENT('tableName') returns the last identity value generated for a specific table in any session and any scope.
The current identity value is larger than the maximum value in the table. Execute DBCC CHECKIDENT (table_name, NORESEED) to determine the current maximum value in the column. Next, specify that value as the new_reseed_value in a DBCC CHECKIDENT (table_name, RESEED,new_reseed_value) command.
Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName Values ('example', 'example', 'example')
send an output parameter like
@newId int output
at the end use
select @newId = Scope_Identity() return @newId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With