Solved

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

Posted on 2008-06-13
7
4,229 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

863 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

26 Experts available now in Live!

Get 1:1 Help Now