父子表的存储过程 [英] Stored procedures for Parent child tables

查看:212
本文介绍了父子表的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子。客户(父母)和订单(孩子)。



客户表格看似

I have two tables. customers(parent) and orders(child).

Customer table looks like

Cid     Name     Address    ProductId
 1        a        b          1
 2        c        d          2



订单


Orders

Oid   productid   productname          cost
1        1        Washing machine      15,000
2        2             TV              8,000





如何为这两个表编写存储过程? customer是父表,order是子表。如果我在客户表中插入数据,它也应插入子表中。扫管笏我的意思是看看在父表中所做的改变,它也应该反映在子表中。



how to write a stored procedure for these two tables? customer is parent table and order is child table. if i insert a data in customer table it should get inserted in child table as well. wat i meant wat ever the changes m gonna make in parent table it should be reflected in child table as well.

推荐答案

我建​​议你为父母添加一个触发器表..这样它将记录插入子表...



有关触发器的信息:触发MSDN [ ^ ]



由Sadique给出的评论是一个有效点。 ..
I Suggest you to add a Trigger for the Parent Table.. such that it insert records into Child Table...

For Information on Triggers : Triggers MSDN[^]

Comment Given By Sadique is a Valid Point...


代表 Sadique KT 添加一个答案,因为问题解决了。



Just adding one answer on behalf of Sadique KT as the problem got solved.

引用:

孩子的产品ID 应该外键

  • 将行插入父表。
  • 获取最大值(ProductId),然后使用 Product Id 在父表中插入第一行后从父表中检索。
  • Product Id in child should be Foreign key.

    • Insert row to parent table.
    • Get max(ProductId), then insert all rows in child with the Product Id retrieved from parent table after insertion of first row in parent table.

    • 这篇关于父子表的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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