Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Add Nulls dynamically to Missing Column Values

user5743038Jul 16 2018 — edited Jul 25 2018

Hi,

Good Morning!

DB Version info.

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit

PL/SQL Release 12.1.0.2.0.

I have to come up with a Solution for the following scenario

I have an Excel File which we Open and Parse and generate Insert  Statements Dynamically into tables based on each Excel Sheet. (1 Worksheet data will be parsed and inserted into an Equivalent Table)

The Excel Parser produces the data structure like this below.

Sheetnum
SheetnameRow_nr
Col_nr
Cell
Cell_type
Data_valFormula
1employee11A1CHARemp_id
1employee12B1CHARfirstname
1employee13C1CHARmiddlename
1employee14D1CHARlastname
1employee15E1DATEhiredate
1employee16F1CHARemail
1employee21A2CHARemp001
1employee22B2CHARAndy
1employee24D2CHARJones
1employee25E2DATE01-JAN-2018
1employee26F2CHARajones@somemail.com
1employee31A3CHARemp002
1employee32B3CHARjames
1employee33C3CHARconan
1employee34D3CHARdoyle
1employee35E3DATE01-FEB-2018

need your help in getting the logic to populate these data into an Oracle table ( say Employee) dynamically.

The Row number 1 is always header info, hence used LISTAGG function to create Column names as comma separated fields, wanted to useLISTAGG function for values list as well, but i could not as my Values may contain null, also the Excel Parser will not list Null values( its a Limitation).

the challenge is I have to create a comma seperated values based data_Val field from Row number 2 as they hold the value for the column headers.

emp_id,firstname,lastname,email cannot be null

The sample data above has 3 rows of data, but I may get more than 1000's of rows to loop.

I am not able to get a logic where magically I can build the insert statements as

     INSERT INTO Column_list VALUES (values_list with Null as values at appropriate places where they are missing)

Say for example the Above three rows should yield 2 insert statements,

     INSERT INTO (emp_id,firstname,lastname,middlename,hiredate,email) VALUES ('emp001','Andy',NULL,'Jones','01-JAN-2018','ajones@somemail.com');

     INSERT INTO (emp_id,firstname,lastname,middlename,hiredate,email) VALUES ('emp002','James',Conan,'Doyle','01-JAN-2018',NULL);

Thanks in advance

This post has been answered by odie_63 on Jul 16 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2018
Added on Jul 16 2018
9 comments
808 views