SQLCMD command, How to save output into log file - sql-server

The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory
However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1)

If you need the output into one common file then you should use the #Abhishek 's answer.
If you need the output into a separate log file for an each input sql file
then you can use -o parameter of sqlcmd command. Your bat file could look like this:
for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause
In this case for
1.sql
2.sql
you will get:
1.sql.log
2.sql.log

You are seeking Command Redirection.
As per your example -
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1
once the execution of the sql script is done the output will be redirected to and appends the command output to the end of file (here sql.log) without deleting the information that is already in the file (>>) and redirects STDERR (2) into STDOUT handle(1) - 2>&1
More information here and here.

Related

Batch file script for sqlcmd

I'm looking for a way to make the below commands a bit more automated. I would like to be able to use just a simple batch file if possible.
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword]
1> drop database datastore
2> go
Have you tried this:
sqlcmd /S localhost\SQLEXPRESS /U [dbusername] /P [dbpassword] <cmds.txt
where cmds.txt is a file containing your commands (the drop and the go commands).

Write a Batch file for automatically delete the OLD IIS log file

Whenever i want to delete the old IIS logs files on my Shared WebServer i run this command on CMD:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
It searches for IIS log files and will delete all of them. For sure it can't delete the current day's log files since they are already opened in IIS and can't be deleted.
It shows me the progress of deleting log files one by one in command prompt console.
i need to set a schedule task to run a BAT file to do this automatically each day.
I have made a bat file and pasted this command within it:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
But when i run the batch file nothing happens and i don't see any result and action.
How to write a batch file for running this Command?
Your help is much appreciate.
Thank you
You need to use %%A in batch files for For loops rather than %A at the command prompt.
del C:\HostingSpaces\u_ex*.log /s
is easier.
I strongly suggest to use the forfiles command for the task:
forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL #File"
Explanation of the switches:
-s or /S : recurse into all subfolders
-p or /P : path
-m or /M : file mask
-d or /D : number of days (-180 = older than 180 days)
-c or /C : command to execute
You can then put it into a Scheduled Task and have it run daily.
For a decent Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post that I wrote on the topic.
The reason that you get error is because that the current day's logs are opened in IIS.
IIS keep the current day's log open and write on it constantly. So you will not be able to delete the current day's logs.
This is the command that worked for me. My websites are all placed in a folder named HOSTINGSPACES and i do this to delete all of the log files (only the current day logs will remain at the end)
# for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
u_ex*.log is the syntax of my logs, your logs may have another syntax, so take care of this.

findstr or grep output to a file

Im in Windows Server 2003 and using below commands to fetch string matching lines in files created today in a specific directory.
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c findstr /i /c:\"Hey Hi\" #FILE" >> txt5.txt
or
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c grep \"Hey Hi\" #FILE" >> txt5.txt
Geeting error 'FINDSTR: Write error' and 'grep write error bad file descriptor' respectively for both commands. So basically the commands work i.e. display the output in screen but unable to redirect the output to a file.
I did not find suitable solution though users reported this same error for different scenarios. Any help is appreciated. Thanks in advance!
You should be able to remove the cmd /c; I don't see any reason you'd need a new copy of the command shell open for the findstr call.
This works for me correctly at the command prompt:
forfiles -m t*.xml -d +0 -c "findstr /i "Item" #file" >> out.txt.
It produces an out.txt file that contains the proper content matching the search criteria.

using sqlcmd in batch file

I've scoured StackOverflow for a few hours, and tried different suggestions for similarly asked questions, but nothing passed the parameters correctly so far (double quotes, ^).
Here's the short version of it:
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i C:\query.sql -s"," | findstr /V /C:"-" /B >c:\output.csv
Basically, I want to pass a rather long parameter containing delimiters. But the only way I see that happen is to use arguments. I'd like to keep it as simple as possible. Is this the only recourse? Can you offer an example how this might work?
Thanks in advance.
I'm not sure if it matters, but I think there should be a space between -s and ","
But more importantly, your pipe construct is wrong. Your sqlcmd command is running in a new window, but your pipe is looking for output from the START command itself in the original window - and there isn't any.
You could get your command to work by escaping the pipe and redirection.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," ^| findstr /V /C:"-" /B ^>"c:\output.csv"
But there is no need to use START at all. Your script can simply execute sqlcmd directly, and everything is much simpler.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," | findstr /V /C:"-" /B >"c:\output.csv"
You might also be running into problems with your password, depending on what characters are used. You might have to quote and/or escape the password, and sqlcmd.exe might have its own escape rules. If it does, then you might have to worry about escaping for both cmd.exe and sqlcmd.exe.
This line should have double quotes around the entire path, in some cases it matters.
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
and the start command needs double quotes at the beginning because it takes the first set as the window title.
start "" /w ....

how to execute all .sql file of current and sub folder using xp_cmdshell

I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j

Resources