Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Tables:

TAccount
AccountNo
Name
Address

all data in TAccount table will generate/insert in billing table

TBilling
BillingNo -on insert auto increment number
AccountNo -on insert get from TAccount table
Name -on insert get from TAccount table
Address -on insert get from TAccount table
Description -on insert get from @paramDescription
DueDate -on insert get from @paramDueDate

at the same time will also insert data to TBillingDetails table

TBillingDetails
No -on insert auto increment number
StatementNo -on insert get from TBilling table (from generated/auto increment)
Details -on insert 'RPT'
Amount -on insert 50.00

TBillingDetails table have multiple details per billing

Thanks in advanced! :)
Posted

1 solution

Write a stored proc that inserts the tables sequentially trapping the SCOPE_IDENTITY() after each insert.

SQL
Declare @AccountID int, @BillingNo int

Insert TAccount...

Set @AccountID = SCOPE_IDENTITY()

Insert TBilling
AccountNo = @AccountID
...
 
Share this answer
 
Comments
Thanks7872 13-Oct-14 4:57am    
Nice. Another option would be to use Triggers.
Mycroft Holmes 13-Oct-14 5:00am    
No triggers are not valid for this solution, you have to assume the user is passing in a shed load of variables to be inserted, you don't want to be passing variables to triggers (not sure you CAN).

Actually triggers used for ANYTHING but auditing are a disaster and should be hunted out of the database.
Thanks7872 13-Oct-14 5:05am    
First table will contain after insert trigger where the inserted values will already be available. Task is just to insert them into another table which will also contain the same trigger(To perform insert into 3rd one).

We can't pass values to trigger but they will be avaialble automatically.
Mycroft Holmes 13-Oct-14 5:10am    
How are you going to get Description to the TBilling table and if you suggest inserting into the TAccount table I'll take your keyboard away from you!
dekdimaya 13-Oct-14 5:33am    
hi thanks, no insert on the TAccount table, will only get data from TAccount table to be inserted to TBilling.

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