[英]Share auto-increment value from SQL stored procedure
I have a stored procedure that inserts data on a table with an auto-increment id (name of 'requestid'). 我有一个存储过程,该过程将数据插入具有自动增量ID(名称为“ 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. 当我使用该过程向其中插入数据时,我想获取auto-increment字段的值,并将其用于对同一存储过程的另一个查询。 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') IDENT_CURRENT(“ dbo.Requests”)
Information about IDENT_CURRENT http://technet.microsoft.com/en-us/library/ms175098.aspx 有关IDENT_CURRENT的信息http://technet.microsoft.com/zh-cn/library/ms175098.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.