Home » excel » python – Update (Oracle) DB with Data from Excel

python – Update (Oracle) DB with Data from Excel

Posted by: admin May 14, 2020 Leave a comment


I have a Oracle table like this: Order_ID, Project_ID.
Order_ID is the primary key.
Project_ID supposed to have a values but left null in many cases.
Now I have a list of Order_ID and Project_ID which is the correct data, but that is in excel.
I have to update my oracle database for matching projects as below:

Bad Data on my Oracle Database:

Order_ID, Project_ID
1000,     20090
1001,     null
1002,     null

Correct Data provided in Excel:

Order_ID, Project_ID
1001,     22565
1002,     25548

Updated Data in Oracle Database should look like:

Order_ID, Project_ID
1000,     20090
1001,     22565
1002,     25548

Please remember I am not looking for the sql statement to do it for each ids/lines, rather imagine I have millions of updates to make and want to automate it with a loop.

I am comfortable to use PL/SQL and Python, but other technologies are also welcome.

Thanks !

How to&Answers:

There are various approaches to load from a excel. You could convert it to a csv and load via SQL* Loader, Use third party packages to load excel, use external table etc. But, a simple method is to use SQL developer. Here is an article from Jeff Smith that describes how to do it.

Import from Excel

For your convenience,
Create a _tmp table with the same structure as that of Orders table and dump the excel onto it.

Create table Orders_tmp as select * FROM Orders where 1=0;

Then use a simple MERGE statement to correct the records in the main Orders table.

USING Orders_tmp s
    ON (t.order_id = s.order_id)
        SET t.project_id = s.project_id;