Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 xp_cmdshell return code

Author  Topic 

ijwelch
Starting Member

2 Posts

Posted - 2008-07-13 : 23:17:49
I need to retrieve the exit code (%errorlevel%) after running a batch file but the result is always 0.

To test I run this:

DECLARE @res INT
EXEC @res = xp_cmdshell 'exit /b 5'
SELECT @res AS 'ExitCode'


and 'ExitCode' is correctly returned as 5.

If I put 'exit /b 5' in C:\test.bat and execute this:

DECLARE @res INT
--Test if result is being filled by presetting @res
SELECT @res=100
EXEC @res = xp_cmdshell 'C:\test.bat'
SELECT @res AS 'ExitCode'


then 'ExitCode' is always 0.

This is running on WindowsXP. Curiously, when run on Windows Vista (32bit) all is fine.

Anybody have any ideas what's going wrong?

ijwelch
Starting Member

2 Posts

Posted - 2008-07-13 : 23:45:46
Sods law. After posting question I solved it myself.

If in batch file and omit /b then all is ok.
Go to Top of Page
   

- Advertisement -