Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Link more than one item for one selling operation ... Expand / Collapse
Author
Message
Posted Tuesday, May 13, 2014 1:04 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
Hello, I have database which that store the sold products in Orders Table and another table called Transaction to
set the time and the total price for the individual order ,
my question is:
How to combined more than one order with only one Transaction number ,
If there is any tables that I should to create or any thing to do to achieve the desired result.

OrderedProducts

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

| Date |TotalPrice| TrnNo |OrderId|
| 12:10/05-11-14 | 10 | 1 | 1 |
| 12:11/05-11-14 | 180 | 1 | 2 |
| 12:16/05-11-14 | 30 | 2 | 3 |
| 12:17/05-11-14 | 90 | 2 | 4 |

C#
string conn = "server=.;uid=sa;pwd=123;database=PharmacyDB;";
SqlConnection con = new SqlConnection();

for (int i = 0; i < dgvSelectedItem.Rows.Count; i++)
{

SqlCommand cmd = new SqlCommand("storedP");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@prdctName", dgvSelectedItem.Rows[i].Cells[0].Value));
cmd.Parameters.Add(new SqlParameter("@ordrdQnty", dgvSelectedItem.Rows[i].Cells[2].Value));
cmd.Parameters.Add(new SqlParameter("@ordrPrice", dgvSelectedItem.Rows[i].Cells[3].Value));
con.ConnectionString = conn;
cmd.Connection = con;
con.Open();
cmd.Parameters.Add(cmd.ExecuteNonQuery());
cmd.ExecuteNonQuery();
con.Close();
}
And this was my stored Procedure:

CREATE proc [dbo].[store]

(@TrnId int
,@prdctName nvarchar(50)
,@ordrdQnty int
,@ordrPrice money
,@OrdrId int
,@TrnDate datetime
,@TrnTotal money)

as
begin transaction trs

INSERT INTO [dbo].[OrderProduct]
(--[TrnId],
[prdctName]
,[ordrdQnty]
,[ordrPrice])
VALUES
(--( SELECT @@IDENTITY from [Transaction] ),
@prdctName
,@ordrdQnty
,@ordrPrice )

if @@ERROR<>0 goto Err_

INSERT INTO [dbo].[Transaction]
(
[OrdrId]
,[TrnDate]
,[TrnTotal])
VALUES
( @OrdrId ,CURRENT_TIMESTAMP ,@TrnTotal)

if @@ERROR<>0 goto Err_

commit tran
return 0

Err_:
rollback
return 1

Could you help me,please, thanks in advanced..
Post #1570157
Posted Tuesday, May 13, 2014 3:50 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
I really want help
Post #1570568
Posted Wednesday, May 14, 2014 8:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 13,202, Visits: 12,683
Hi and welcome to the forums. In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

In general you have a lot of issues with that stored proc. First of all is the name. It really should have a name that is descriptive of what it does. "store" does not give any indication what that stored proc is doing. Maybe something more like "AddItemToOrder"?

The next issue is using reserved words like Transaction as object names. This is not a good idea and makes your coding a lot more painful to work with. I would think that is something like a CustomerOrder. Then your detail table could be something like CustomerOrderDetail.

Be careful when using the money datatype. It is ok until you start doing math with that column, you will end up with rounding errors. This is because money is an approximate datatype. Numeric(9,2) would be my preference.

Next you really should look at using try/catch instead of named code blocks and goto statements.

All that aside I am not really sure what your actual question is here. If I am correct that you have header and detail tables I would split this into two stored procedures. The first one will create the Transaction row and with an output parameter you can retrieve the value of the identity. Then you will pass that as a parameter to the second stored proc that adds rows to the OrderProduct table.

For the record I am not a fan of storing a calculated price in the header. I would much prefer to just calculate it when it is needed. That eliminates all the hassle and pain of trying to keep it in synch.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1570870
Posted Wednesday, May 21, 2014 2:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
Thank you @Sean Lange for your reply and your valuable words I will edit my table and SP names ,
well I've faced difficulties to join more than one OrderItemDetail record with one OrderItem record ,
This my tables statement :
CREATE TABLE [dbo].[OrderProduct](
[OrdrId] [int] IDENTITY(1,1) NOT NULL,
[prdctName] [nvarchar](50) NULL,
[ordrdQnty] [int] NULL,
[ordrPrice] [money] NULL,
[TrnId] [int] NULL)


INSERT INTO [dbo].[OrderProduct]
([prdctName]
,[ordrdQnty]
,[ordrPrice]
,[TrnId])
VALUES
('ABC',
4,
20,
1)


CREATE TABLE [dbo].[Transaction](
[TrnId] [int] IDENTITY(1,1) NOT NULL,
[OrdrId] [int] NOT NULL,
[TrnDate] [datetime] NOT NULL,
[TrnTotal] [money] NOT NULL)


INSERT INTO [dbo].[Transaction]
([OrdrId]
,[TrnDate]
,[TrnTotal])
VALUES
(1
,05-14-2014
,20)




Post #1573034
Posted Wednesday, May 21, 2014 7:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, May 26, 2014 10:38 PM
Points: 3, Visits: 15
Hello,

Your problem is example of many to many relationship designing.

For maintaining it you need to create one more table which will contain only OrderID and TransactionID and it will work like a bridge table between these two tables.

Please check below links for referenace -

http://discoversql.blogspot.in/2012/04/database-modelling-many-to-many.html

http://www.techrepublic.com/article/get-it-done-solve-a-many-to-many-relationship-problem-in-microsoft-access/#.

Regards,
Gourav Saxena
Data Warehouse Consultant
GouravSaxena1987@gmail.com
Post #1573154
Posted Thursday, May 22, 2014 2:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:50 AM
Points: 79, Visits: 267
I would also suggest you review the C# code slightly; it would be better to create the parameters explicitly with the datatypes used by the stored procedure, eg.


SqlParameter parm00 = new SqlParameter("@ordrdQnty", SqlDbType.Int);
parm00.Value = (int)dgvSelectedItem.Rows[i].Cells[2].Value;
comm.Parameters.Add(parm00);



and, if you haven't already done so, put the SQL access in a try/catch/finally structure.
Post #1573459
Posted Friday, May 23, 2014 8:52 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
I want to fill the two tables at the same time but I don't know how to do that, It's not many to many relationship I think , they are many items belong to one selling operation , that what I have to reach to, thanks for your help
Post #1574078
Posted Friday, May 23, 2014 8:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 13,202, Visits: 12,683
StarterProgrammer (5/23/2014)
I want to fill the two tables at the same time but I don't know how to do that, It's not many to many relationship I think , they are many items belong to one selling operation , that what I have to reach to, thanks for your help


Let me see I can paraphrase the situation you are trying to accomplish.

You have a set of item for an order. The problem is the order table and the order details both need to be created at the same time and you are struggling with how to get the identity value from the insert into the order table so you can use it that in the order detail table?

There are couple ways you can handle this. Are both of these insert operations in the same stored procedure? If the answer is yes then the answer lies with SCOPE_IDENTITY(). If the answer is no then the answer lies with the OUTPUT clause.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1574082
Posted Friday, May 23, 2014 9:05 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
It's yes ,but I've used IDENTITY as SET @TrnId = 'SCOPE_IDENTITY', but it doesn't work I don't know how
Post #1574084
Posted Friday, May 23, 2014 9:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:13 PM
Points: 13,202, Visits: 12,683
StarterProgrammer (5/23/2014)
It's yes ,but I've used IDENTITY as SET @TrnId = 'SCOPE_IDENTITY', but it doesn't work I don't know how


Can you post the code you are working with? My guess is that you are pretty close.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1574087
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse