DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

How to compose SQL queries based on values from CSV file?

Issue:

How to compose SQL queries based on values from CSV file?

Solution:

SQL> create table MY_TABLE(a varchar2(100), b varchar2(100));

[oracle@myora ~]$ tail Some_Input_CSV_file
Some Data A 1,Some Data B 1
Some Data A 2,Some Data B 2
Some Data A 3,Some Data B 3
Some Data A 4,Some Data B 4
Some Data A 5,Some Data B 5
Some Data A 6,Some Data B 6
Some Data A 7,Some Data B 7
Some Data A 8,Some Data B 8
Some Data A 9,Some Data B 9
Some Data A 10,Some Data B 10
[oracle@myora ~]$

[oracle@myora ~]$ cat Some_Input_CSV_file | awk -F, ‘ { printf(“insert into MY_TABLE values(trim(\x27%s\x27), trim(\x27%s\x27));\n”, $1, $2); } ‘ > RunMe.sql

[oracle@myora ~]$ tail RunMe.sql
insert into MY_TABLE values(trim(‘Some Data A 1’), trim(‘Some Data B 1’));
insert into MY_TABLE values(trim(‘Some Data A 2’), trim(‘Some Data B 2’));
insert into MY_TABLE values(trim(‘Some Data A 3’), trim(‘Some Data B 3’));
insert into MY_TABLE values(trim(‘Some Data A 4’), trim(‘Some Data B 4’));
insert into MY_TABLE values(trim(‘Some Data A 5’), trim(‘Some Data B 5’));
insert into MY_TABLE values(trim(‘Some Data A 6’), trim(‘Some Data B 6’));
insert into MY_TABLE values(trim(‘Some Data A 7’), trim(‘Some Data B 7’));
insert into MY_TABLE values(trim(‘Some Data A 8’), trim(‘Some Data B 8’));
insert into MY_TABLE values(trim(‘Some Data A 9’), trim(‘Some Data B 9’));
insert into MY_TABLE values(trim(‘Some Data A 10’), trim(‘Some Data B 10’));
[oracle@myora ~]$

[oracle@myora ~]$ sqlplus myuser/mypwd@myinst

SQL> @RunMe.sql

…

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

SQL> commit;

Commit complete.

SQL> exit
Enter fullscreen mode Exit fullscreen mode

Top comments (0)