Run All SQL Files in a Directory

Run all SQL files in a directory

Create a .BAT file with the following command:

for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
pause

If you need to provide username and passsword

for %%G in (*.sql) do sqlcmd /S servername /d databaseName -U username -P 
password -i"%%G"

Note that the "-E" is not needed when user/password is provided

Place this .BAT file in the directory from which you want the .SQL files to be executed, double click the .BAT file and you are done!

DOS command to execute all SQL script in a directory and subdirectories

The following will get you started

for /r %f in (*.sql) do echo %f

Run from the command line that will print the names of all the SQL files in the current directory and all sub directories.

Then substitute sqlcmd -i%f for echo %f to execute the scripts.

Hope this helps.

Find all .sql-Files in a Directory and execute them with sqlcmd

The FOR /F works differently. It runs a command. Jusr *.sql is not a command. Also, I suspect that you want to concatenate stdout and stderr output using >>.

@ECHO ON
SET "SERVER_NAME=*SERVER*"
SET "DB_NAME=*DB*"
SET "LOGIN_ID=*USER*"
SET "PASS=*PW*"

FOR /F "delims=" %%G IN ('DIR /B "*.sql"') DO (
sqlcmd -S %SERVER_NAME% -d %DB_NAME% -U %LOGIN_ID% -P %PASS% -i "%%~G" >> LOG_lastrun.txt 2>> LOG_errors.txt
)

pause

Run all SQL files in multiple directories

Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:

Syntax: START "title" [/D path] [options] "command" [parameters]

Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.

If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run
.

Next Script1.cmd should work and close started command windows:

start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

How to execute all SQL files in a folder using a BATCH file and also log the result of each file in the SQL filename .log?

for /r "your_path" %%i in (*.sql) do (
sqlcmd -i "%%i" -o "log\%%i.log"
)

How to run SQL files in a directory in order by file name in PSQL

Loop through:

YOUR_DIR="/path/to/your/directory"
for file in $YOUR_DIR/*; do
psql "${file}"
done


Related Topics



Leave a reply



Submit