Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 3 data tables to update database

Invoice Table primary key is InvoiceNo

InvoiceProduct Table primary key is InvoiceProductNo and foreign key InvoiceNo

InvoiceProductExp Table primary key is InvoiceProductExpNo and foreign kyes are InvoiceNo and InvoiceProductNo

3 Data Tables Data entry would be for example is

Invoice (InvoiceNo,...)

(0001,...)

InvoiceProduct (InvoiceProductNo, InvoiceNo,...)

(1,0001,...)

(2,0001,...)

InvoiceProductExp (InvoiceProductExpNo,InvoiceProductNo,InvoiceNo,...)

(1,1,0001,...)

(2,1,0001,...)

(3,2,0001,...)

(4,2,0001,...)

the problem is i liked to use sql server generated AutoNumber or Identity column for all primary keys of 3 tables

how can i prepare for insert statements

Insert Into InvoiceProductExp values (auto_number, ?, ?)
Posted
Updated 19-Jul-13 17:55pm
v2

1 solution

insert first row to Invoice table like

SQL
CREATE PROCEDURE PROCEDURENAME
@Parameter1 dataType,
@Parameter2 dataType,
@Parameter3 dataType,
@InvoiceNo BigInt,
@LastId BigInt=0 OUTPUT

AS 
BEGIN
INSERT INTO Invoice(Field1,Field2...)Values(@Parameter1,@Parameter2....)
SELECT @InvoiceNo= MAX(InvoiceNo) FROM Invoice
SET @LastId=@InvoiceNo
END

Now you will get invoice no for inserted row..in @LastId you can pass it to second query ..
and follow same method in second sp to get the primary key of second table...
 
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