2) Example of running batch file using T-SQL i) Running standalone batch file (without passed parameters) EXEC master..xp_CMDShell 'c:findword.bat'
ii) Running parameterized batch file
DECLARE @PassedVariable VARCHAR(100) DECLARE @CMDSQL VARCHAR(1000) SET @PassedVariable = 'SqlAuthority.com' SET @CMDSQL = 'c:findword.bat' + @PassedVariable EXEC master..xp_CMDShell @CMDSQL
Book Online has additional examples of xp_cmdshell
A. Returning a list of executable files
B. Using Windows net commands
C. Returning no output
D. Using return status
E. Writing variable contents to a file
F. Capturing the result of a command to a file
This issues was brought to me by our Sr. Network Engineer. While running operating system level de-fragmentation using either windows de-fragmentation or third party tool it always skip all the MDF file and never de-fragment them. He was wondering why this happens all the time.
The reason MDF file are skipped all the time in de-fragmentation because they are in use when SQL Server is running. Windows operating system de-fragmentation skips all the file in are currently in use.
After discovering this the real question was how to de-fragment when files are in use. Steps are Stop the Server, Re-start, keep the SQL Server services off and do de-fragmentation.
What if Server can not be taken off-line? In that case, nothing can be done. In my company SQL Server runs on redundant configuration – they are mirrored real time as well fail-over clustering is configured. We took one server down and system ran on safely on fail over server while we ran de-fragmentation on other server. We repeated the same order for all server. We see significant difference in our operating system performance as well as database response time.
One more thing to note, operating system needs more than 15% empty space to do optimal de-fragmentation.
Just for fun I will type my conversation with our Sr. Network Engineer. He is very smart person.
Sr. Network Engineer : Hey Pinal, MDF files does not de-fragment every night.
Pinal : May be because they are in use. Needs to alternate and de-fragment on cluster, what do you think?
Sr. Network Engineer : Yeah! Thanks. It will be good test for fail-over as well. Will do tonight.