Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting into multiple tables from ASP from, need help ASAP

Status
Not open for further replies.

belcom125

Programmer
Nov 17, 2004
45
CA
I have 2 tables in the SQL 2000 database, Customers and CustomerAddress. The reason is because one customer can have multiple physical locations. I am building an ASP app that automates sales order process.

Anyway... I have a single form that has to create a new customer record but Company and Address are in different tables. When I do the first insert in automatically creates a new ID number for that customer but then I need to insert address info into Address table with that CustomerID that was just created. Basically I can't do both at once because I need to assign just created CustomerID to the FK field in the address table. Does it make sense ? :eek:))

What's the best way to handle this ?
 
After your first insert you can use:
Code:
SELECT Scope.Identity() as [IDNum]
[/code}]

That will retrieve your PK for you.. then you can use that value to do the second insert.

I love small animals, especially with a good brown gravy....
 
can you please explain what exactly scope.identity() does? I can't find info on it.
 
Why not create a stored proc? Send all the fields you need to the stored proc and just run it as a transaction.

Code:
begin transaction
insert into myTable
   (myFields...)
values
   (myAspFields...)

select @myPk = scope_identity()

insert into mySecondTable
    (FK, myFields....)
values
    (@myPk, myAspFields...)
commit transaction
 
From BOL:

Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top