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 wOOdy-Soft 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, how to get the PK.

Status
Not open for further replies.

darwin101

Programmer
Mar 7, 2001
156
US
hi all
I need to insert client records from a csv file into multiple tables. the client table creates the pk,clientId. how would I go about getting that clientId to insert the remainder of the records into different tables? if you could point me at an example of similar code or instructions I would be most appreciative. I am thinking a cursor would be envolved, but I have never written one.

thnks
 
use scope_identity()

here is an sample script
Code:
create table test1 (id int identity primary key, someDate datetime)

create table test2(id int, id2 int FOREIGN KEY (id2) REFERENCES test1(id))
GO
declare @pk int
insert test1 values(getdate())

select @pk = scope_identity()
insert test2 values(1,@pk)

GO
select * from test1

select * from test2
GO

drop table test2,test1
GO

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top