How to execute several .SQL files from console

Yesterday I needed to run an older version of a project just to make a few changes, for that I needed to update my old code for that project and run all the scripts (.SQL files) to update the database. A colleague gave me a line of code to get the task done faster than executing each file manually, here it is:

for %G in (*.sql) do exit | sqlplus username/password@hostname:port/SID @ ./%G

Now, to be able to get this to work you will need to open cmd.exe and navigate to the folder that contains all the .SQL files that you want to run. Once there, you type the line above replacing the words in italics with your own values.

Example: Let’s say my SQL scripts are in

C:Temp

What I need to do in the console is to navigate to that folder

c:>
c:>cd temp
c:Temp>

Then I would type

c:Temp>for %G in (*.sql) do exit | sqlplus userhp/passhp@127.0.0.1:1521/XE @ ./%G

Hit Enter and it will start to run all the scripts in that folder (*.sql), if everything went OK you will not see any errors, after that it just disconnects from the DB.

Leave a Comment

Your email address will not be published. Required fields are marked *