Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to sqlplus in a shell script and run SQL scripts

I have a .sql file, which is a bunch of oracle pl/sql commands and I want to create a shell script to run these commands.

Suppose that user/pass@server is my credentials. What will be the shell script to do such a task?

like image 949
Farshid Avatar asked Apr 23 '12 09:04

Farshid


People also ask

How do I run a SQL script in SQL shell?

sql file from PUTTY, we first connect to DB using syntax <sqlplus> and then DB credentials. Then, we will give the file execution command as <@file_name. sql> and once the file is executed, we will give <exit> to come out of db and then remaining process.

How do I run a SQL script in sqlplus?

Answer: To execute a script file in SQLPlus, type @ and then the file name. The above command assumes that the file is in the current directory. (ie: the current directory is usually the directory that you were located in before you launched SQLPlus.) This command would run a script file called script.


2 Answers

For example:

sqlplus -s admin/password << EOF whenever sqlerror exit sql.sqlcode; set echo off  set heading off  @pl_script_1.sql @pl_script_2.sql  exit; EOF 
like image 77
NetBear Avatar answered Sep 18 '22 19:09

NetBear


Wouldn't something akin to this be better, security-wise?:

sqlplus -s /nolog << EOF CONNECT admin/password;  whenever sqlerror exit sql.sqlcode; set echo off  set heading off  @pl_script_1.sql @pl_script_2.sql  exit; EOF  
like image 28
Blaine DeLancey Avatar answered Sep 20 '22 19:09

Blaine DeLancey