Click here to Skip to main content
14,878,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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.
Posted
v2
Comments
Sadique KT 13-Aug-13 3:25am
   
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.
ARUN K P 13-Aug-13 3:31am
   
ok fine
   
Is your problem solved?
ARUN K P 13-Aug-13 3:36am
   
yeah tadit.
   
Okay. Great news. As a part of moderation, I have added one answer.

Please accept that and up-vote. We need to change the status of this question as "solved".

Thanks,
Tadit
ARUN K P 13-Aug-13 3:47am
   
done tadit

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...
   
Comments
ARUN K P 13-Aug-13 3:47am
   
ok fine
Just adding one answer on behalf of Sadique KT as the problem got solved.

Quote:
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.
Comments
ARUN K P 13-Aug-13 3:52am
   
ok
   
:)
Thanks for accepting and up-voting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900