Bulk Insert from MS SQL to Oracle linked server table using Nextval

Posted on 2008-06-13
Last Modified: 2013-12-07
We are integrating our MS SQL server app with our new Oracle Financial database (10G).  We are connecting to our Oracle database with a linked server using a Oracle OLE db driver.  We need to insert data to an Oracle table from our own SQL table.  The Oracle table has a primary key that we need to use the Oracle nextval function to determine the unique record key for our inserted records.  However when we do this in a set manner we are only returning 1 value and thus don't have a unique id for each record.  We would like to avoid using a cursor on the SQL side and inserting one row at a time.  I have considered using the SQL rownumber function EX - "row_number() over (order by sCustID asc) as rownum" and adding that to the returned value, but I don't know if that will work if other possible users or processes are also inserting to this table at the same time.  

Is there another way to get the nextval to return a new value from Oracle for each record I'm inserting without having to resort to a cursor or looping on the SQL side?  Or do I need to go with my row_number option?
	(select * from openquery(ORACLE_AR_CNV_NEW_O, 'Select xxar.XX_AR_CUSTOMER_INTERFACE_ID_S.nextval from dual')) as ID,
FROM	app.vwAutoCustomer_CreditApp_Process

Open in new window

Question by:InfoTeam
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 21778327
You could always setup a temp table on the Oracle side so you can bulk insert into that table and have a Insert Trigger run that will then move the data into the right table.  This offloads the line processing to the Oracle box instead of the SQL box and is fairly easy to do.  I'm not sure, but you may also be able to do a before insert function on the real table in Oracle that, if the value of myKey is null, then use the nextVal in it's place, then insert,

LVL 35

Expert Comment

ID: 21778392
Instead of using the nextval in your insert query, create a trigger that generates the nextval  and puts it in the table.

Author Comment

ID: 21782385
I can only make changes on the MS SQL side for our inserts.  I can only post to the table and not make any changes to the Oracle side.
Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation


Accepted Solution

scgstuff earned 500 total points
ID: 21782445
Then I think you will need to do a row iteration to get the value for each to be inserted.  Unless you wanted to make something on the SQL side which would have a blank entry for the record number and yuo could have the script do the nextval through the recNum is null entries in SQL, then do the bulk upload.

LVL 35

Expert Comment

ID: 21784788
If you make changes on the SQL side and do not use the sequence on the Oracle side, then the sequence will no longer correspond to the correct id.  If you cannot make changes on the Oracle side, then how are you going to fix that?  The application that writes to this table will start generating duplicate keys.

Author Comment

ID: 22024664
We would have to build a SQL loop, which we have done using while, to use the Oracle nextval function to retrieve the data we need.  We are not going to make changes to the Oracle side, we just need to use what they have given which we have conceeded that we have to use line by line processing instead of set processing.  

Author Closing Comment

ID: 31466908
The basic answer is that if no changes can be made on the Oracle side, there is not a way to for SQL to handle without building a loop.  Not the answer I wanted, but I think the only one at this point.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question