Execute multiple files at one go using a single bat file - sql

I am using a batch file to execute multiple sql files.
So I have created a bat file like:
osql -S ServerName -U user -P password -d DBTest -i C:\SQLFILES\Test1.sql
pause
The above code executes a single file Test1.sql and if I need to execute the next file I have to again modify the bat file and change the file name.I am having 10 such sql files and I want to execute them one after another. Is there any way to do this at one go?

Well, what you could definitely do is give your BAT file a parameter (so you don't have to constantly change the BAT file contents...):
ExecSQL.bat:
osql -S ServerName -U user -P password -d DBTest -i %1
pause
and then you can call this batch file like this:
c:\> ExecSQL C:\SQLFILES\Test1.sql
and then
c:\> ExecSQL C:\SQLFILES\Test2.sql
and so forth

Related

How to execute the SQL Script through Batch Script Command from SQL?

I am trying to execute the .sql file from batch script, but not able to execute.
Script has multiple scripts such as Create Table , Stored Procedure separated by GO statement.
I tried with the below script, however script was not executed.
DB Servername : localhost\SQL2017
Authentication : Windows
sqlcmd -E -S localhost\SQL2017\MyDatabase -i C:\SQLScripts\TestScript.sql
Try this instead:
sqlcmd -E -S localhost\SQL2017 -d MyDatabase -i C:\SQLScripts\TestScript.sql
You need to use the -d parameter to specify the database name. The -S parameter should contain only the server name and the instance.

Run large SQL query from a bat file

I am trying to run a monthly extract from a SQL Express DB using a .bat file called from windows scheduler and the SQL query is large (about 50 lines) and I can't work out how to include such a large script in the bat file without it all being flattened out into one (long) single row of text. Is there a carriage return (or similar) command that I can use to keep the query in the readable SQL format like I have below?
SELECT a.[Application_Number]
,case when c_MNI >0 then
100*(a.monthly_living_allowance +
a.New_Home_Loan_Repayment_Amount +
c_mc)/c_MNI
else 0 end as nsr_calc
,a.[FIN_Total_Net_Service_Ratio] as NSR
,a.Total_Annual_Income_Gross_Total as Annual_Gross
,a.fin_total_annual_income_net1 as Annual_Net
,C_MNI as MNI
,C_MC as MC
,a.[monthly_living_allowance] as MLA
,a.[Manual_MLA]
,a.[Manual_MLA_Flag] ....etc etc...
You can use sqlcmd in your bat file. Put your SQL script in a file, let's say it's DoSomeQuery.sql. Then call sqlcmd in your bat file like this:
sqlcmd -S servername -U user -P password -d DB_Name -i DoSomeQuery.sql
If you have multiple sql files, you can use for command in your bat file like this:
for /r . %%f in (*.sql) do sqlcmd -S servername -U user -P password -d DB_Name -i "%%f"

Mercurial SQL and Windows Command

Example of the contents I require a .cmd to contain
mkdir Output
sqlcmd -S serverName -d dbName -E -i "FILE LOCATION HERE" -o Output\Message.log
sqlcmd -S serverName -d dbName -E -i "FILE LOCATION HERE" >> Output\Messages.log
.
.
.
pause
Specifics: I have a SQL repo and need to generate a code that will take all changes from last revision to this revisionand output the above example.
As far as Mercurial is concerned,
hg status -I re:.*\.sql$ -am --rev 3:7
will give the relation of all files with .sql extension added or modified after changeset 3 and up to changeset 7. You can then massage the output into the desired script with your favourite text processing tools.
N.B.: Blindly running all modified SQL scripts, specially if you intend to use this procedure more than once, seems rather dangerous, unless you are sure all the operations are idempotent.

How to save a SQLCMD code as a executable file

I have a set of SQLCMD codes which are to be saved as exe files so that people can just execute the file rather than copy paste the code and run the command in command line.
Write your sql code to file for example script.sql and than create a batch file (for example run.bat) with below command
sqlcmd -U user -P passwrod -S SerwerName -i C:\script.sql

How to execute a sql script from a url?

I have 10 sql scripts lying on 10 SVN urls.
I want to write a single sql script which execute those 10 svn sql scripts.
for example, http://svn/s1.sql, http://svn/s1.sq2, ....
I want to write a single sql which does like execute http://svn/s1.sql, execute http://svn/s2.sql, etc
How can I do it?
You can run all the .SQL files using sqlcmd
First Create a Batch file and Paste the below coding in that batch file :
sqlcmd -S ServerName -U Username -P password -i c:\s1.sql -o C:\s1.txt
sqlcmd -S ServerName -U Username -P password -i c:\s2.sql -o C:\s2.txt
sqlcmd -S ServerName -U Username -P password -i c:\s3.sql -o C:\s3.txt
sqlcmd -S ServerName -U Username -P password -i c:\s4.sql -o C:\s4.txt
Execute the Batch file from SQL Server like below..
EXEC master..xp_CMDShell 'c:filename.bat'
You can also refer the Below link for running batch file..
SQL SERVER – Running Batch File Using T-SQL – xp_cmdshell bat file
You would need to write a program that downloads the files, reads them in line by line, appends them internally and executes the whole batch.
It would be a huge security hole, if you could execute SQL scripts by calling a url in your browser.