如何使用表值参数插入多行然后返回它们的 ID? [英] How can I use a Table-Valued Parameter to insert multiple rows and then return their IDs?

查看:29
本文介绍了如何使用表值参数插入多行然后返回它们的 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要将大量 (100+) 行插入到数据库中.一旦他们被插入到数据库中,我需要插入他们的孩子,这些孩子有一个外键引用回到孩子们.

In my application, I have a large of number (100+) of rows that I need to insert into the database. Once they get inserted into the database, I need to insert their children, which have a foreign key reference back to the children.

我想知道是否有一种方法可以编写一个可以插入所有这些行并将它们的 ID 返回到我的应用程序的存储过程?

I'm wondering if there's a way to write a stored procedure that can insert all of those rows and return their IDs back to my application?

推荐答案

您已经用表值参数标记了您的问题 - 您可以将这些参数传递给存储过程以插入到数据库中.

You have tagged your question with table-value-parameters - you can pass one these to the stored procedure for inserting to the database.

您可以使用OUTPUTINSERTED 逻辑表的子句以获取新的 ID 值并从存储过程返回这些值.

You can use the OUTPUT clause with the INSERTED logical table to get the new ID values and return those from the stored procedure.

这篇关于如何使用表值参数插入多行然后返回它们的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆