17

I have to run multiple SQL script file in one go.

Like every time i have to write command in SQLPLUS

SQL>@d:\a.txt 
SQL>@d:\a2.txt
SQL>@d:\a3.txt
SQL>@d:\a4.txt

is there any way put all file in one folder & run all script file in one go without missing any single file like @d:\final.txt or @d\final.bat

1
  • have you tried windows batch file ? (open notepad, write the commands in sequence line by line) and then save the file as FileName.bat (make sure you have selected All Files option from bottom of the save dialogue. Running the batch file by double click shall do the trick. Or you may need a little advanced batch commands inside that bat file Commented Mar 27, 2014 at 7:31

8 Answers 8

14

There is no single SQL*Plus command to do that, but you can create a single script that calls all the others:

Put the following into a batch file

@echo off
echo.>"%~dp0all.sql"
for %%i in ("%~dp0"*.sql) do echo @"%%~fi" >> "%~dp0all.sql"

When you run that batch file it will create a new script named all.sql in the same directory where the batch file is located. It will look for all files with the extension .sql in the same directory where the batch file is located.

You can then run all scripts by using sqlplus user/pwd @all.sql (or extend the batch file to call sqlplus after creating the all.sql script)

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

4 Comments

@rahuljain: sorry, I have no idea what you are talking about.
i ran the batch file. all.sql file is created fine. i open this file this file have code @"D:\test_SQL\Patch03.01.486.txt" @"D:\test_SQL\Patch03.01.487.txt" @"D:\test_SQL\Patch03.01.488.txt" if i run file like @all.sql is this will working fine? i have to run this all txt files in prod
This helps me a lot, great answer
Fun fact: if you first create all.sql and then put all *.sql files in it, you'll end up having all.sql pointing to all.sql) Therefore you better delete that reference to avoid eternal circular execution
8

If you're using gnu linux, you could use process substitution:

sqlplus USERNAME/PASSWORD@DOMAIN < <(cat a.txt a2.txt a3.txt a4.txt) 
# ... or a for loop on input files, inside the process substitution

Alternatively, you can create a .pdc file and list your sql scripts:

-- pdc file
@a.txt;
@a2.txt;
@a3.txt;
@a4.txt;

and call sql plus:

sqlplus USERNAME/PASSWORD@DOMAIN < my_scripts.pdc

1 Comment

I tried this solution and it works. However i haven many files numbered file1.sql, file2.sql .. fileN.sql and they are listed in all.sql file like all.sql [email protected] [email protected] --... [email protected] The problem is the files are not executed in the order in which they are listed. How can i force it to use the order of the files like listed in the all.sql file ? Thanks for your answer,
3

Some tricks and command can help you to generate master.sql file and you can run from that location.

 c:\direcotory_location\dir *.sql /-t /b >master.sql

Go to the parent directory open master.sql open using notepad++ remove master.sql line and use regular expression to replace

   \n  with \n @

go to cmd From cmd

    C:\root_directory\sqlplus user/password @master.sql

I find this process very convenient if i have 30 to 40 scripts placed in a single directory.

Comments

3

Use *.PDC extension file like this

install.pdc file content

whenever sqlerror exit sql.sqlcode

prompt started!

prompt 1.executing script 1
@@install/01.script_1.sql

prompt 2.executing script 2
@@install/02.script_2.sql

prompt 3.executing script 3
@@install/03.script_3.sql

prompt finished!

where @@install/ points in which directory is the SQL script located

Comments

2

It might be worth the time to write a shell script that runs multiple files.

#!/bin/ksh
sqlplus user/password@instance <<EOF
@a.txt
@a1.txt
exit
EOF

For more on the syntax, look into Here Document

Comments

1

here is similar solution but you do not have to iterate and to have special formated an sql file names. You compose an one sql file and run it once.

cat table_animal.sql > /tmp/temp.sql
cat table_horse.sql >> /tmp/temp.sql
cat table_fish.sql >> /tmp/temp.sql
sqlplus USERNAME/PASSWORD@DOMAIN @/tmp/temp.sql

1 Comment

Similarly, if you want to concatenate all of the SQL files in a directory, you can use cat *.sql > temp.sql.
1

For Windows try copy /b *.sql +x final.sql

sqlplus user/password @final.sql

Comments

-2

Special Thanks to Joseph Torre

sqlplus login/password@server @filename

reference link

1 Comment

This handles just a single sql file for which the topic owner already had a solution.

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.