gpt4 book ai didi

sql-server - 在SQL Server中,执行Insert时是否可以获取 "id"的记录?

转载 作者:行者123 更新时间:2023-12-02 08:04:17 24 4
gpt4 key购买 nike

在 SQL Server 2005 中,我的表中有一个“id”字段,该字段的“Is Identity”属性设置为“Yes”。因此,当在该表上执行插入时,“id”会自动设置为下一个递增整数。执行插入时是否有一种简单的方法来获取“id”设置的内容,而不必在插入后立即执行 Select 语句?

duplicate of:
Best way to get identity of inserted row?

最佳答案

至少在 .Net 中,您可以一次性向服务器发送多个查询。我在我的应用程序中执行此操作:

command.CommandText = "INSERT INTO [Employee] (Name) VALUES (@Name); SELECT SCOPE_IDENTITY()";
int id = (int)command.ExecuteScalar();

就像魅力一样。

关于sql-server - 在SQL Server中,执行Insert时是否可以获取 "id"的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com