[英]How can i set the id from one table to another table
I have a question, let's say I have two tables I am doing an insert operation to the first table with the stored procedure, but how do I insert the auto incrementing ID in the first table in the other table?我有一个问题,假设我有两个表,我正在使用存储过程对第一个表执行插入操作,但是如何在另一个表的第一个表中插入自动递增 ID?
Would be good if you provided one example of what you are trying to achieve.如果您提供一个您想要实现的目标的示例,那就太好了。
To get the inserted Id by the store procedure, you could use SCOPE_IDENTITY.要通过存储过程获取插入的 Id,您可以使用 SCOPE_IDENTITY。
SCOPE_IDENTITY() : returns the last identity value generated in the current scope. SCOPE_IDENTITY() :返回当前 scope 中生成的最后一个标识值。
For instance:例如:
INSERT INTO.... VALUES(); SELECT SCOPE_IDENTITY();
This query will insert into a the given table and will return the id inserted.此查询将插入到给定的表中,并将返回插入的 ID。
You can also use Select @@IDENTITY , this will return the last id inserted regardless the scope.您还可以使用Select @@IDENTITY ,这将返回最后插入的 ID,而不管 scope。
INSERT INTO.... VALUES(); SELECT @@IDENTITY;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.