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.
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.


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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

710 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