How to save a SQLCMD code as a executable file - sql

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

Related

How to execute a file.sql (script) and save the results in a file in my computer using CMD (SQLCMD)?

I need to run a script saved in my computer through CMD (SQLCMD) and save the result in other file in my computer.
Use SQLCMD to run a script from command line, see:
https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-2017
To save output to a file, look into using pipes for your output, see:
Redirect Windows cmd stdout and stderr to a single file
or
https://helpdeskgeek.com/how-to/redirect-output-from-command-line-to-text-file/
You can run the script and then create another backup.
You can try mssql-scripter that is a multiplatform SQL command line tools such as sqlcmd.
Use bcp
bcp "SELECT * FROM TABLE" queryout file.csv -t";" -SSERVERNAME -T -c -e errors.txt

Run .sql file from batch file

I'm new to batch files and would like to execute sql script from the batch file. Please can someone assist.Thanks in advance.
You can use sqlcmd.exe from the DOS prompt. If you run sqlcmd -? from the DOS prompt the program will tell you the command line parameters. Here's an example
sqlcmd -S MyDbServer -d DatabaseName -E -i "MyScript.sql"
The -E tells sqlcmd to use "trusted authentication" meaning your Windows login. If you're not using Windows Authentication then you'll want to use the -U and -P parameters.

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