Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am inserting mutiple sales orders for a single sales details.
The tables that I have created are

Parent Table
SQL
CREATE TABLE Tbl_Sales(SalesID INT CONSTRAINT pk_Tbl_Sales_SalesID PRIMARY KEY IDENTITY(101,1),CustomerID INT CONSTRAINT FK_Tbl_Customers_CustomerID FOREIGN KEY (CustomerID) REFERENCES Tbl_Customers(CustomerID),DateOfSale DATE DEFAULT GETDATE(),SalesName VARCHAR(50) CONSTRAINT Un_SalesName UNIQUE)


Child Table
SQL
CREATE TABLE Tbl_SalesDetails(SalesDetailID INT CONSTRAINT pk_SalesID PRIMARY KEY IDENTITY(1,1),ModelID int constraint fk_SalModelID references Tbl_Models(ModelID),CustomerID int Constraint fk_CustomerID references Tbl_Customer(CustomerID),Quantity int,Unit varchar(20) default 'Pcs',Price decimal(18,2),Amount decimal(18,2),ForwardQuantity int,ForwardPrice decimal(18,2),AmountPaid decimal(18,2),DateOfSale date default getdate())

And The Temp Table
SQL
CREATE TABLE Tbl_SalesItemDetails(SalesItemID INT CONSTRAINT pk_SalesItemID PRIMARY KEY IDENTITY(1,1),ModelID int constraint fk_ItemModelID references Tbl_Products(ModelID),CustomerID int Constraint fk_SalesCustomerID references Tbl_Customers(CustomerID),Quantity int,Unit varchar(20) default 'Pcs',Price decimal(18,2),Amount decimal(18,2),ForwardQuantity int,ForwardPrice decimal(18,2),AmountPaid decimal(18,2),TotalAmount DECIMAL(18,2),DateOfSale date default getdate(),SalesName VARCHAR(50))


Now I have written store procedure to insert the data
SQL
ALTER PROCEDURE Sp_CommitSales
AS
BEGIN
DECLARE @SalesID INT

BEGIN TRAN
    INSERT INTO Tbl_Sales (SalesName,CustomerID, TotalAmount)
    SELECT  top 1 SalesName,CustomerID,TotalAmount FROM Tbl_SalesItemDetails  order by SalesItemID DESC

    set @SalesID=SCOPE_IDENTITY();

    INSERT INTO Tbl_SalesDetails(ModelID,CustomerID,Quantity,Unit,Price,Amount,ForwardQuantity,ForwardPrice,TotalAmount,DateOfSale,SalesID)
    SELECT ModelID,CustomerID,Quantity,Unit,Price,Amount,ForwardQuantity,ForwardPrice,TotalAmount,DateOfSale,@SalesID
    FROM Tbl_SalesItemDetails

COMMIT TRAN
END


When I first insert records into temp table records inserting but if I execute store procedure multiple records are inserting where i am doing wrong please suggest me??
Posted
Updated 18-Sep-14 15:37pm
v2
Comments
Gihan Liyanage 18-Sep-14 23:59pm    
Cant Understand the last two statements of you. Can you please explain bit. What is the final problem of you?
Gihan Liyanage 19-Sep-14 0:01am    
The Other thing is you have asked same question before you design these tables. I have answered you with detailed description if I am correct. This may be report as re post because the question heading is same. If you have time plz edit the question header. People may misunderstand.

1 solution

I guess, you have to use IDENT_CURRENT instead of SCOPE_IDENTITY.
Details of the same are available in one of the article :
Difference between @@IDENTITY, SCOPE_IDENTITY, IDENT_CURRENT[^]

Thanks,
Baliram Suryawanshi
 
Share this answer
 

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