插入父表和子表而不循环(一次全部)
问题描述:
我正在使用C#和sql server
父表名== tblOrderMaster
儿童表名== tblOrdereEtails
我可以制作一个商店程序,在其各自的表中插入父和多个孩子
没有在前端使用循环,甚至在后端使用
如果是,那么如何?
提前感谢
i am working with C# and sql server
parent table name == tblOrderMaster
Child table name == tblOrdereEtails
can i make one store procedure to insert parent and multiple childs in thier respective tables
without using loop in front end and even in backend
if yes then how ?
thanks in advance
答
是的,这是可能的。您可以一次发送包含子数据列表的父数据。 (从最后没有循环)
您需要创建存储过程来接受对象数组。根据sql server版本,您可以有不同的解决方案。请查看以下答案以获取更多信息。
如何将数组传递到SQL Server存储过程中 [ ^ ]
您可以插入父数据并按 SCOPE_IDENTITY(),请查看以下答案
insert-into-parent-child-tables-in-sql-server [ ^ ]
接下来你需要使用父项同时向子表插入数据key和子数据数组
Yes it is possible. You can send Parent data with List of Child data at once. ( there is no loop in from end)
you need to create stored procedure to accept array of objects. depending on sql server version you can have different solutions. check below answers for more information.
How to pass an array into a SQL Server stored procedure[^]
you can insert parent data and take the primary key bySCOPE_IDENTITY()
, check below answer
insert-into-parent-child-tables-in-sql-server[^]
next you need to insert data to child table using both parent key and the array of child data