PRB: File Compare Utility Fails to Run from Xp_cmdshell (281340)



The information in this article applies to:

  • Microsoft SQL Server 7.0

This article was previously published under Q281340

SYMPTOMS

The file compare utility fails to run from the xp_cmdshell extended stored procedure.

CAUSE

The SQL Server process does not create a WinStation for the fc command. If you pipe the output to a Nul console, you get the results you expect.

WORKAROUND

To work around this problem you can either:
  • Pipe the output to a NUL console. For example:
    xp_cmdshell 'fc c:\test1.txt c:\test.txt <NUL:'
    					

    -or-

    xp_cmdshell 'fc c:\test1.txt c:\test.txt <NUL: >c:\output.txt'
    					
    -or-

  • Create a SQL Server Agent job of type "Operating System Command" and direct the output to a file. Then, call the job from the Query Analyzer with the sp_start_job stored procedure.

MORE INFORMATION

To see the failure occur, use the following steps:

  1. Create two files named test1 and test and make sure that the files have data in them.
  2. Log in as system administrator (sa) to the SQL Server server, and then execute the following query from Query Analyzer:
    xp_cmdshell 'fc c:\test1.txt c:\test.txt'
    					

    -or-

    xp_cmdshell 'fc c:\test1.txt c:\test.txt > c:\output.txt'
    					
    These steps return a "null" as the result and no error occurs. The xp_cmdshell extended stored procedure executes correctly for other DOS commands.


If you use the following example from SQL Server 2000 Books Online
DECLARE @result int

EXEC @result = xp_cmdshell 'fc c:\test1.txt c:\test.txt'


IF (@result = 0)

    PRINT 'Success'

ELSE

    PRINT 'Failure'
				
the result is Failure, which indicates that the file compare does not work from within xp_cmdshell.

A similar problem might occur if you use the XCOPY utility in an xp_cmdshell extended stored procedure.

Modification Type:MajorLast Reviewed:8/15/2002
Keywords:kbprb KB281340