sqlsql-serveroracle-databaseoracle10glinked-server

Selecting a sequence NEXTVAL for multiple rows


I am building a SQL Server job to pull data from SQL Server into an Oracle database through a linked server. The table I need to populate has a sequence for the name ID, which is my primary key. I'm having trouble figuring out a way to do this simply, without some lengthy code. Here's what I have so far for the SELECT portion (some actual names obfuscated):

SELECT (SELECT NEXTVAL FROM OPENQUERY(MYSERVER, 
    'SELECT ORCL.NAME_SEQNO.NEXTVAL FROM DUAL')), 
     psn.BirthDate, psn.FirstName, 
     psn.MiddleName, psn.LastName, c.REGION_CODE
FROM Person psn
LEFT JOIN MYSERVER..ORCL.COUNTRY c ON c.COUNTRY_CODE = psn.Country

MYSERVER is the linked Oracle server, ORCL is obviously the schema. Person is a local table on the SQL Server database where the query is being executed.

When I run this query, I get the same exact value for all records for the NEXTVAL. What I need is for it to generate a new value for each returned record.

I found this similar question, with its answers, but am unsure how to apply it to my case (if even possible): Query several NEXTVAL from sequence in one statement


Solution

  • I ended up having to iterate through all the records and set the ID value individually. Messy and slow, but it seems to be the only option in this scenario.