| Answer | Return no output
This example uses xp_cmdshell to execute a command string without returning the output to the client.
USE master
EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',
NO_OUTPUT
Use return status
In this example, the xp_cmdshell extended stored procedure also suggests return status. The return code value is stored in the variable @result.
DECLARE @result int
EXEC @result = xp_cmdshell 'dir *.exe'
IF (@result = 0)
PRINT 'Success'
ELSE
PRINT 'Failure'
Write variable contents to a file
This example writes the contents of the @var variable to a file named var_out.txt in the current server directory.
DECLARE @cmd sysname, @var sysname
SET @var = 'Hello world'
SET @cmd = 'echo ' + @var + ' > var_out.txt'
EXEC master.xp_cmdshell @cmd
F. Capture the result of a command to file
This example writes the contents of the current directory to a file named dir_out.txt in the current server directory.
DECLARE @cmd sysname, @var sysname
SET @var = 'dir/p'
SET @cmd = @var + ' > dir_out.txt'
EXEC master.xp_cmdshell @cmd |