I have a stored procedure that inserts data on a table with an auto-increment id (name of 'requestid'). As I insert data into it using the procedure, I want to take the value of the auto-increment field and use it on another query to the same stored procedure. How do I do that? Thank you in advance for your time.
CREATE PROCEDURE [dbo].[sp_Create_new_request]
@employeeid INT ,
@requestdate DATETIME ,
@deliverdate DATETIME ,
@totalcost MONEY
AS
BEGIN
INSERT INTO dbo.Requests
(
EmployeeID ,
RequestDate ,
DeliverDate ,
TotalCost
)
VALUES
(
@employeeid ,
@requestdate ,
@deliverdate ,
@totalcost
)
END
Try
CREATE PROCEDURE [dbo].[sp_Create_new_request]
@employeeid INT ,
@requestdate DATETIME ,
@deliverdate DATETIME ,
@totalcost MONEY,
requestid INT = NULL OUT
AS
BEGIN
INSERT INTO dbo.Requests
(
EmployeeID ,
RequestDate ,
DeliverDate ,
TotalCost
)
VALUES
(
@employeeid ,
@requestdate ,
@deliverdate ,
@totalcost
)
SET @requestid = SCOPE_IDENTITY();
END
您不应使用自动增量,而应使用序列来增量,然后可以在任何地方重用该值。
You can use also :
IDENT_CURRENT ('dbo.Requests')
Information about IDENT_CURRENT http://technet.microsoft.com/en-us/library/ms175098.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.