Friday, December 9, 2011

Running Sql in a single file


More often we get to the scenario where 

100's of files in a directory or a list of files mentioned in a txt file and we need to run them.

The way that comes to mind spontanesously is to run one by one, but how about run it in a single shot where you can know what you are running and also can spool it ,so that you know which one errored.

Below are the two approaches where you can create a single file and run it in the sqlplus with spool.


For files in a directory

  • Get to the directory where files are (example : /home/srini/function )
  • In the command prompt run the following 
  • >runall.sql
    for i in `ls -1`
    do
    echo "PROMPT Running File $i ...." >>runall.sql
    echo "@@$i" >>runall.sql
    ## echo "@$PWD$i" >>runall.sql -- For having the full path of the sql
    done;
  • Run the runall.sql in the Sqlplus .

For files in a text File

  • Go to the location of the file having the list of all .sql (example : example : /home/srini/function.txt )
  • In the command prompt run the following 
>runall.sql
for i in `cat package.dat`
do
echo "PROMPT Running File $i . ..." >>runall.sql
echo "@@$i" >>runall.sql
## echo "@$PWD$i" >>runall.sql -- For having the full path of the sql
done;
  • Copy this runall.sql to the location where you have all the .sql and run it in sqlplus