简体   繁体   English

如何将一张表的id设置到另一张表

[英]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.

相关问题 如何从另一个表中获取基于ID的一个表数据 - How to get One Table data on the basis of ID from another table 如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中? - How can I insert all rows from one table into another table with Linq-to-SQL? 如何根据另一个表中的 Id 获取文本框值? - How can I get textbox value based on Id in another table? 将 ID 从一个表插入到另一个 MVC 表(1:N) - Insert ID from one table to another MVC table (1: N) 如何从一个表中获取 Id 并将其保存到 ASP.NET Core 中的另一个表,存储库模式 - How to get an Id from one table and save it to another table in ASP.NET Core, repository pattern 如何在另一个视图(表)中显示来自一个 ViewModel 的数据? - How can I show data from one ViewModel in another View (table)? 如何从表中删除一行并将其通过SQL语句插入另一行? - How can I remove one row from a table and insert it into another via a SQL statement? 如何将行从一个数据库中的表移到另一个数据库? - How do I move a row from a table in one database to another? 数据表 C# - 如何根据 ID 从一个表中获取详细记录及其从另一表中的关联子记录 - Datatable C# - How to get detail record from one table and its associated sub record from another table based on ID 如何将记录从一张表更新到另一张表 - how to update the records from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM