Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 INSERT INTO help w/ utilizing the identity ID

Author  Topic 

dreardon
Starting Member

2 Posts

Posted - 2007-07-05 : 01:41:05
I'm having a problem with the logic in a SQL query that I need to run.

I'm selecting 4 columns from an each record in my old database and inserting 2 into tableA and 2 into tableB of a new database. TableA and tableB both have identities on with the self-incrementing values, albeit they are starting at different numbers due to old data. As shown below, I need to get the ID number of the row entered in tableA in order to populate a pointer field in tableB. How should I write the SQL query. My example below is basically pseuocode since I'm sure I'm pretty far off. I've played around with @@IDENTITY, but can't get it to work.

INSERT INTO DB1.dbo.tableA
values(valA, valB)
INSERT INTO DB1.dbo.tableB
values([unique id entered in the first insert], valC, valD)
SELECT valA, valB, valC, valD
from DB2.dbo.users

Can someone help me with this? Thanks.

Dan

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-07-05 : 01:47:24
[code]INSERT INTO DB1.dbo.tableB
values(Scope_Identity(), valC, valD)[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

dreardon
Starting Member

2 Posts

Posted - 2007-07-05 : 19:21:44
Thanks for the help.
Go to Top of Page
   

- Advertisement -