Run SQL Query from Linux command as shown below:
Posted by Mir Sayeed Hassan on December 25th, 2017
Run SQL Query from Linux command as shown below:
Method 1:
[oracle@testdb ~]$ echo "select table_name from user_tables;" | sqlplus -s mir/mir123 TABLE_NAME ------------------------------ TEST1 TEST2
[oracle@testdb ~]$ echo "select * from test1;" | sqlplus -s mir/mir123 ENO ---------- 1 2
[oracle@testdb1 ~]$ echo "select count(*) from test1;" | sqlplus -s mir/mir123 COUNT(*) --------- 2
Method 2: — Create a .sql file & perform any action ex: insert few records init
Note: Create your own directory & place the .sql file in it
[oracle@testdbmir]$ cd /tmp
[oracle@testdbtmp]$ mkdir mir
[oracle@testdbmir]$ vi insert.sql
[oracle@testdbmir]$ cat insert.sql -------- You can perform any action as per your requirement insert into mir.test1 values(3); insert into mir.test1 values(4); commit;
[oracle@testdb mir]$ sqlplus -s mir/mir123 </tmp/mir/insert.sql 1 row created. 1 row created. Commit complete.
Verify:
[oracle@testdb mir]$ echo "select * from test1;" | sqlplus -s mir/mir123 ENO ---------- 1 2 3 4