0

I have n .sql files for example 1.sql, 2.sl, 3.sql.... n.sql in a folder named sql_scripts. I want to execute all those files using a single shell script. Whats the easiest and the most preferred way to do so?

3 Answers 3

1
cat 1.sql 2.sql 3.sql ... n.sql |sqlplus username/password@sid

Or

cat *.sql|sqlplus username/password@sid

?

Sign up to request clarification or add additional context in comments.

Comments

0

Loop through the files, executing each one in turn:

for file in /home/user/sql_scripts/*.sql; do
    echo "Executing $f.."
    sqlplus user/pass @"$f"
done

You can run a file in SQLPlus by using the @filename.sql notation.

Comments

0

as explained above by Josh,

using @sqlscript.sql in sqlplus is the best way but this technique might not work in all databases, instead you can modify your script like,

su - <db user>
connect to database <your db name > 

(this again varies according to your db i.e. in db2 it is like db2 connect to , in oracle connect to etc)

for file in /home/user/sql_scripts/*.sql; do
    echo "Executing $f.."
    db2 -tvf "$f" <db2>
    @"$f"         <oracle/sqlplus etc>   
done

modify above script as per your db and requirements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.