Here is a method of capturing the output of a command executed by the system shell. It is typically called from the SA account, and used to automate maintenance tasks. I do not recommend that you allow other users to send statements to a command shell.
--- BEGIN CODE
DECLARE @s varchar(255);
-- our command
SET @s = 'DIR C:\TEMP\*.EXE' ;
-- the temporary table
CREATE TABLE #tCmdOutput( output varchar(1024) NULL ) ;
-- capturing output from the command
INSERT INTO #tCmdOutput( output )
EXEC Master..XP_CmdShell @s ;
-- delete blank lines
DELETE FROM #tCmdOutput WHERE output is null ;
-- show output as a result set
SELECT * FROM #tCmdOutput ;
-- clean up our session
DROP TABLE #tCmdOutput ;
--- END CODE
--- BEGIN CODE
DECLARE @s varchar(255);
-- our command
SET @s = 'DIR C:\TEMP\*.EXE' ;
-- the temporary table
CREATE TABLE #tCmdOutput( output varchar(1024) NULL ) ;
-- capturing output from the command
INSERT INTO #tCmdOutput( output )
EXEC Master..XP_CmdShell @s ;
-- delete blank lines
DELETE FROM #tCmdOutput WHERE output is null ;
-- show output as a result set
SELECT * FROM #tCmdOutput ;
-- clean up our session
DROP TABLE #tCmdOutput ;
--- END CODE
Comments