Solved

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

Posted on 2008-06-13
7
4,220 Views
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?
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

0
Comment
Question by:InfoTeam
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:scgstuff
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,

Shawn
0
 
LVL 34

Expert Comment

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

Author Comment

by:InfoTeam
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Accepted Solution

by:
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.

Shawn
0
 
LVL 34

Expert Comment

by:johnsone
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.
0
 

Author Comment

by:InfoTeam
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.  
0
 

Author Closing Comment

by:InfoTeam
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.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Viewers will learn how the fundamental information of how to create a table.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now