Click here to Skip to main content
14,925,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 tables in dataset
when i click save button,
i want to add these tables to database tables using data adapter
all these 3 tables primary keys are sql generated auto number.

relation ships Invoice, InvoiceProduct , InvoiceProductExp tables are:
InvoiceNo has many InvoiceProductNo
InvoiceProductNo has many InvoiceProductExpNo

the following code can not solve these relaionship

SQL
DECLARE @InvoiceNo INT
DECLARE @InvoiceProductNo INT

INSERT INTO Invoice ([Date])
VALUES (GETDATE())

SELECT @InvoiceNo = SCOPE_IDENTITY()

INSERT INTO InvoiceProduct([InvoiceNo])
VALUES (@InvoiceNo)

SELECT @InvoiceProductNo = SCOPE_IDENTITY()

INSERT INTO InvoiceProductExp ([InvoiceProductNo], [InvoiceNo])
VALUES (@InvoiceProductNo, @InvoiceNo)
Posted
Comments
pradiprenushe 21-Jul-13 0:51am
   
what problem you face?
deva936 22-Jul-13 22:24pm
   
i want insert statement to insert data from dataTable to DataBase Table for 3 tables in the question

1 solution

You need to use: @@IDENTITY(T-SQL)[^] instead SCOPE_IDENTITY()
SQL
DECLARE @InvoiceNo INT
DECLARE @InvoiceProductNo INT

INSERT INTO Invoice ([Date])
VALUES (GETDATE())

SELECT @InvoiceNo = @@IDENTITY()

INSERT INTO InvoiceProduct([InvoiceNo])
VALUES (@InvoiceNo)

SELECT @InvoiceProductNo = @@IDENTITY()

INSERT INTO InvoiceProductExp ([InvoiceProductNo], [InvoiceNo])
VALUES (@InvoiceProductNo, @InvoiceNo)
   

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