SQL插入主要和详细记录

SQL插入主要和详细记录

本文介绍了SQL插入主要和详细记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表''采购订单''abd''采购订单产品''。



我使用.NET c#程序调用存储过程。



我想首先为''采购订单'生成id和插入记录。然后插入采购订单产品的记录,因为它包含生成的ID。



如果只调用单个存储过程,我可以使用TRY CATCH和ROLLBACK来确保两个表的更新同步。但是,我不知道如何将信息列表(即采购订单产品的所有记录)传递到单个商店程序中。



例如将以下列表作为参数传递

每个产品包括名称,数量,金额

ProductA,1,$ 20

ProductB,3,$ 28

...



所以我假设每次为一个产品调用insert product程序,但是它不能回滚''Purchase的插入行如果在插入''产品'时发生任何错误,请订购'。



是否有任何可以解决我的情况的做法,以便我可以确保所有记录(订购和产品)将被添加或者不会添加任何记录并且错误已知?

I got two Tables ''Purchase Order'' abd ''Purchase Order Products''.

I am using .NET c# program to call store procedure.

I would like to generate id and insert record for ''Purchase Order'' first. Then insert records for ''Purchase Order Products'' since it includes the ids generated.

If just calling a single store procedure, I can use TRY CATCH and ROLLBACK to make sure updates of two tables in sync. However, I do not know how to pass a list of information (i.e. all records for Purchase Order Product) into a single store procedure.

e.g. pass the following list as parameters
each Product include name, qty, amount
ProductA, 1, $20
ProductB, 3, $28
...

So I assume calling insert product procedure each time for one product, but then it cannot rollback the inserted row of ''Purchase Order'' if any errors occur in inserting ''Products''.

Is there any practice that can solve my situation so I could make sure all records (Order and Products) will be added or no records will be added and error is known?

推荐答案




这篇关于SQL插入主要和详细记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:23