如何一次将记录插入多个表中

问题描述:

我想在多个表中插入记录,但是假设我有两个表,一个有主键,另一个有外键,一次将记录插入这两个表.plz帮帮我?

i want insert record in multiple table but suppose i have two table one have primary key another have foreign key and insert record into this two table at one time.plz help me?

最好的方法是创建一个存储过程,如果可以 - 并在其中使用一个事务。

如果没有,那么在你的普通代码中使用一个事务。 br />
但是在任何一种情况下,过程都是相同的:首先写入主键表项,然后使用主键表中的新数据写入外键项。如果任何一个失败,你回滚事务,否则你提交它。



我无法提供代码的确切细节,因为它对于存储过程会有所不同,或VB或C#...但谷歌无论如何都会帮助你。
The best way is to create a stored procedure, if you can - and use a transaction within that.
If not, then use a Transaction in your "normal" code.
But the procedure is the same in either case: write the Primary key table entry first, then write the Foreign key entry using the new data from the Primary key table. If either fails, you rollback the transaction, otherwise you commit it.

I can't give exact details on the code, because it will be different for a stored procedure, or VB or C#...but Google will help you with the parts anyway.