Link more than one item for one selling operation ...

  • 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.Cells[0].Value));

    cmd.Parameters.Add(new SqlParameter("@ordrdQnty", dgvSelectedItem.Rows.Cells[2].Value));

    cmd.Parameters.Add(new SqlParameter("@ordrPrice", dgvSelectedItem.Rows.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..

  • I really want help

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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)

  • 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

  • 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.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.

  • 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

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • It's yes 🙂 ,but I've used IDENTITY as SET @TrnId = 'SCOPE_IDENTITY', but it doesn't work I don't know how

  • 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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • this my stored procedure and I'm sorry about names

    ALTER proc [dbo].[storedP]

    (@prdctName nvarchar(50)

    ,@ordrdQnty int

    ,@OrderID int

    ,@ordrPrice money

    ,@TrnDate datetime

    ,@TrnTotal money)

    as

    BEGIN TRY

    begin transaction trs

    DECLARE @TrnId int

    SET @TrnId = 'SCOPE_IDENTITY'

    INSERT INTO [dbo].[OrderProduct]

    ([OrdrId],

    [prdctName]

    ,[ordrdQnty]

    ,[ordrPrice]

    ,[TrnId])

    VALUES

    (@OrderID,

    @prdctName

    ,@ordrdQnty

    ,@ordrPrice

    ,@TrnId

    )

    INSERT INTO [dbo].[Transaction]

    (

    [OrdrId]

    ,[TrnDate]

    ,[TrnTotal])

    VALUES

    ( @OrderId ,CURRENT_TIMESTAMP ,@TrnTotal)

    commit tran

    return 0

    END TRY

    BEGIN CATCH

    rollback

    return 1

    END CATCH

  • StarterProgrammer (5/23/2014)


    this my stored procedure and I'm sorry about names

    No need to apologize about the names to me. The are painful but you are the one who has to suffer with them. 😉

    I think the main issue is you don't understand how IDENTITY works. How are you getting the value for @OrderID to call this procedure? This is the new value for OrdrId in OrderProduct right? That means since you are using IDENTITY you don't know it yet.

    The next issue is you have declared a variable @TrnId as an int and are trying set the value to a string literal. I am quite confused by TrnID. What is that for? It seems like that is maybe the primary key in Transaction. Why is there a column in the parent table for that?

    Something like this might be closer but your structures seems to be a bit out of alignment too.

    ALTER proc [dbo].[storedP]

    (

    @prdctName nvarchar(50)

    ,@ordrdQnty int

    --,@OrderID int

    ,@ordrPrice money

    ,@TrnDate datetime

    ,@TrnTotal money

    ) as

    BEGIN TRY

    begin transaction trs

    INSERT INTO [dbo].[OrderProduct]

    (

    --[OrdrId], You don't want to insert an explicit value here

    [prdctName]

    ,[ordrdQnty]

    ,[ordrPrice]

    --,[TrnId]

    )

    VALUES

    (

    --@OrderID,

    @prdctName

    ,@ordrdQnty

    ,@ordrPrice

    --,@TrnId

    )

    INSERT INTO [dbo].[Transaction]

    (

    [OrdrId]

    ,[TrnDate]

    ,[TrnTotal]

    )

    VALUES

    (

    SCOPE_IDENTITY() ,

    CURRENT_TIMESTAMP ,

    @TrnTotal

    )

    commit tran

    return 0

    END TRY

    BEGIN CATCH

    rollback

    return 1

    END CATCH

    You should check BOL and read about the IDENTITY property so you understand what it does and how it works.

    http://msdn.microsoft.com/en-us/library/ms186775.aspx

    _______________________________________________________________

    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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • .

  • .

  • It seems it works but it asked about the time which is CURRENT_TIMESTAMP , it supposed to get the current time of the system automatically ,right!

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply