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

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?
INSERT INTO OPENQUERY(ORACLE_AR, 
	'Select
	XX_AR_CUSTOMER_INTERFACE_ID, 
	ACCOUNT_NUMBER,
	CREDIT_APPROVE_DATE,
	CREDIT_APPLICATION_TYPE,
	CREATED_BY,
	CREATION_DATE,  
	STATUS_CODE,                
	FROM APPS.XXAR_CUSTOMER_INTERFACE')
 
SELECT 
	(select * from openquery(ORACLE_AR_CNV_NEW_O, 'Select xxar.XX_AR_CUSTOMER_INTERFACE_ID_S.nextval from dual')) as ID,
	sCustID,
	Credit_Approval_Date,
	Credit_App_Type,
	Created_By_CTS,
	Creation_Date,
	Status_Code
 
FROM	app.vwAutoCustomer_CreditApp_Process

Open in new window

InfoTeamAsked:
Who is Participating?
 
scgstuffConnect With a Mentor Commented:
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.

Shawn
0
 
scgstuffCommented:
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,

Shawn
0
 
johnsoneSenior Oracle DBACommented:
Instead of using the nextval in your insert query, create a trigger that generates the nextval  and puts it in the table.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
InfoTeamAuthor Commented:
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.
0
 
johnsoneSenior Oracle DBACommented:
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.
0
 
InfoTeamAuthor Commented:
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.  
0
 
InfoTeamAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.