Run large SQL query from a bat file - sql

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"

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.

SQLcmd output contains also unwanted input SQL statements

I created a sql script that retrieve some information from database as a table format,
I have create a sqlcmd script that execute that query file,
but in the output file I have the queries present in the .sql file + the results that are saved,
result file http://prntscr.com/7e4j5i
I want only to have the result in that file
SQLCMD -S pcname\SQLEXPRESS -v VITI=%vl% -E -w 166 -e -i C:\db\input.sql -o C:\db\Book1.csv -s"," -w 700 -es
How can I do it?
Do I have to change something in the .sql file to exclude the queries from the results file?
Remove the -e parameter.
The -e parameter will echo the input (i.e. your SQL).
For info, see here.

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.

Executing series of SQL commands on the command line

I want to run a series of SQL statements against a SQL Server 2005 database from the command line.
When I launch 1st statement
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
it is going to prompt window 1> from there after I am unable to proceed further through script.
How to give input to 1> prompt I mean giving next SQL statement
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
and finally exit to that prompt
I tried with && but I guess that is for only commandline commands.
You ae looking for the -Q switch on the sqlcmd tool (don't use osql on sqlserver 2005 or higher) (type sqlcmd /? to see all options) or lookit up on msdn
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -Q "BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP"
Alternatively you can create a sqlscript file where you put all the sql statements in you want to execute. Assuming you name your file myscript.sql the osql command would go like this:
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -i myscript.sql
Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).
You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Linux). For example:
script.TXT:
#echo off
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
exit
Execute previous "script" this way:
cmd < script.TXT
If you perform this test, please report the result...
Antonio

Execute multiple files at one go using a single bat file

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