473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Insert data into 2 tables one-to-many relation

6



I want to write a Stored Procedure for these 2 tables ,
I have this SP but it doesn't achieve my point..


Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE proc [dbo].[Sp_Insert]
  3. @prdctName NVARCHAR(50),
  4. @ordrdQnty INT,
  5. @ordrPrice MONEY
  6. --@TrnId INT
  7.  
  8. AS BEGIN TRAN
  9. declare @TrnId int 
  10.  
  11. set @TrnId = SCOPE_IDENTITY()
  12. --IDENT_CURRENT
  13.  
  14. Insert into [OrderProduct]
  15. (prdctName ,ordrdQnty ,ordrPrice,TrnId )
  16. values   (@prdctName,@ordrdQnty,@ordrPrice ,@TrnId)
  17.  
  18.  
  19. declare @OrdrId int
  20. declare @TrnTotal int
  21.  
  22. set @OrdrId=@@IDENTITY
  23. --set @TrnTotal =  @ordrPrice
  24. --@ordrdQnty *
  25.  
  26. if @@ERROR<>0 goto ERR_
  27.  
  28.  
  29. Insert into [Transaction] ( OrdrId,TrnDate )--,TrnTotal
  30.  
  31. values (@OrdrId,GETDATE() )--,@TrnTotal)
  32. if @@ERROR<>0 goto ERR_
  33.  
  34. commit tran 
  35. return 0
  36.  
  37. ERR_:
  38.  rollback tran
  39.  return 1
  40.  
  41. GO
  42.  
  43.  
Jun 23 '14 #1
10 1534
Rabbit
12,516 Expert Mod 8TB
External storage sites are blocked by our firewalls. Attach the image to the thread itself.

You said the SP doesn't achieve your point. But you haven't told us what the "point" is so there's nothing we can help you with.
Jun 23 '14 #2
Begood
6
Thank you for your reply, I have 2 tables one of them is OrderProduct (orderId P.K.,ProdctName,Quantity,Price)which store the list of items that the customer want one by one , the second table I named it Transaction(TrnId P.K.,orderId f.K.,Datetime) which it suppose to group the list of Orders by one TrnId, So the realtion between these 2 tables is one(TrnId)-to-many(Orders).
In my SP I just store the orders without specifying TrnId ,
Actully I want to program the selling form.
I don't know what do you mean about the image!
I hope to be my point is clear now , and I'm sorry for my first bad description..
Jun 23 '14 #3
Rabbit
12,516 Expert Mod 8TB
You still haven't explained what problem you're having. You said you want to store the order without transaction. Which doesn't make sense because now you have no way of tying the order to the transaction. But aside from that, you still need to explain what problem you're having implementing that. Is it giving you an error? What's the error code? Is it not working the way you want? What is it doing instead? How should it work? What is some sample data and results?

My comment about the image is that you used a site to store the image. My firewall blocks that site, so I can't see the image. Attach the image to the forum thread.
Jun 23 '14 #4
Begood
6
My SP store the orders in Transaction without specify the TrnId, there is no error, but I need to link orders with TrnId when It saved in Transaction table..

OrderedProduct

|Price|Quantity|ProductName|OrderId|
|10 | 2 | A | 1 |
| 80 | 1 | C | 2 |
| 30 | 5 | B | 3 |
| 60 | 3 | B | 4 |
========================================
Transaction

| Date | TrnNo |OrderId|
| 12:10/05-11-14 | 1 | 1 |
| 12:11/05-11-14 | 1 | 2 |
| 12:16/05-11-14 | 2 | 3 |
| 12:17/05-11-14 | 2 | 4 |
Jun 23 '14 #5
Rabbit
12,516 Expert Mod 8TB
Is your question that you don't know how to get the OrderID that you just inserted?
Jun 24 '14 #6
Begood
6
my question is what is the required editing that SP need to store the Orders in Transaction table with one TrnId to let me then write query to calculate the total amount of the list of orders as group of TrnId.
Suppose there is customer who buy (1-a,2-b,3-c) as names of products ) these products should store to OrderProduct Table with the quantity and Price for each one products so I have 3 fields in OrderProduct then these Orders supposed to be stored in Transaction Table so I need to join these 3 orders with one TrnId as it one operation that happen at the same deal and for one customer ..
I hope I could making my idea clearer ..
Jun 24 '14 #7
Rabbit
12,516 Expert Mod 8TB
If you only want one transaction record but link it to multiple order records, you need to store a transaction id in the order table. Not the other way around like you have now because that's impossible.
Jun 24 '14 #8
Begood
6
How I will get Transaction Id value If I stored it in the Order table, the Order table is filled with data at first ..
Jun 24 '14 #9
Rabbit
12,516 Expert Mod 8TB
Insert the transaction first
Jun 25 '14 #10
Begood
6
Thank you ...........
Jun 25 '14 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: shiyuan | last post by:
Dear Sir: We have created a table with a column of composite type. how can we insert values in this table. for exemple: create table aa (a int); create tabel bb (b aa); how to insert data ...
9
by: Hi5 | last post by:
Hi, Any Idea how, I can make an Insert statement to insert data into 6 different tables, that are all holding all data of my database? Is there any example? I would be grateful if you could...
1
by: Pratik Gupte | last post by:
I have created a database in .mdf format, but I am unable to insert data into its tables. Can anybody help how to insert data using ASP.Net 2.0 using SQL Server 2005 Express edition in windows...
2
by: tezza98 | last post by:
Hi im creating a function to insert data from a MYOB file into an Access database. what im trying to do is copy entire tables from MYOB into ACCESS. Im using a simple Access Database with so there...
0
by: hooliovelasko | last post by:
Sorry for my English. I use C#. I have two tables with relation(category an product) i want to load them in treeview. I load category table - parent nodes but i want to load and product table - child...
17
by: msmjsuarez | last post by:
Hello, I need help... How to automatically insert data to mysql database without submit button using php? Is there a way on this? thanks a lot.
2
by: msmjsuarez | last post by:
Hello. Is it possible to insert data to two tables at one time using one form only? I'm using php and mysql. Thanks advance.
2
by: shivasusan | last post by:
Hi! I have two tables in my database (userinfo, logininfo) - userinfo table has five fields (uname, pws, con_pws, squest, sans) - logininfo table has then six fields (fname,lname,dob,add1,...
3
by: southpawjoe | last post by:
Hi, I would like to insert data into two related tables in a SQL Server 2008 database. I created the following stored procedure but it has a problem. When I add a record, the State table gets...
1
by: Bappa Dey | last post by:
Hi.. // i have a database in mysql, which has 14 tabels, among them i can put/insert data into 12 tables successfully using php, but 2 tables are not taking any data.// <from></from> is ok ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.