Link to home
Start Free TrialLog in
Avatar of jdana
jdanaFlag for United States of America

asked on

Bit to Boolean?

I built a simple sproc that runs an executable using xp_cmdshell.  I want to be able to identify errors when the executable is called.  The variable XPCmdShellResult contains a bit value.  When the compiler hits the IF clause it balks, presenting the following:

An expression of non-boolean type specified in a context where a condition is expected, near 'BEGIN'.

If I modify the IF clause to the following:

   IF @XPCmdShellResult > 0

The procedure runs properly, but I should be able to @XPCmdShellResult to behave as a Boolean without the comparison operator.  (It's cleaner.)
CREATE PROCEDURE dbo.CallRTXMLExportTool
AS
DECLARE 
   @XPCmdShellCommand varchar(200),
   @XPCmdShellResult bit   
   SET @ErrorHint = 'Not Configured'
   SET @XPCmdShellCommand = 'C:\RTXMLExportTool.exe'
   EXEC @XPCmdShellResult = xp_cmdshell @XPCmdShellCommand, NO_OUTPUT
   IF @XPCmdShellResult -- Problematic code here.
   BEGIN
     -- Error code not shown.
   END
GO

Open in new window

Avatar of Sharath S
Sharath S
Flag of United States of America image

Try with CASE statement.
CREATE PROCEDURE dbo.CallRTXMLExportTool
AS
DECLARE
   @XPCmdShellCommand varchar(200),
   @XPCmdShellResult bit  
   SET @ErrorHint = 'Not Configured'
   SET @XPCmdShellCommand = 'C:\RTXMLExportTool.exe'
   EXEC @XPCmdShellResult = xp_cmdshell @XPCmdShellCommand, NO_OUTPUT
   CASE WHEN @XPCmdShellResult  = 1 --code here
        ELSE --code here
   END  -- Problematic code here.
   BEGIN
     -- Error code not shown.
   END
GO
 
SOLUTION
Avatar of Steve Hogg
Steve Hogg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CREATE PROCEDURE dbo.CallRTXMLExportTool
AS
DECLARE
   @XPCmdShellCommand varchar(200),
  @XPCmdShellResult bit  
   SET @ErrorHint = 'Not Configured'
  SET @XPCmdShellCommand = 'C:\RTXMLExportTool.exe'
  EXEC @XPCmdShellResult = xp_cmdshell @XPCmdShellCommand, NO_OUTPUT
  CASE WHEN ISNULL(@XPCmdShellResult,0)  = 0 --code when your variable is FALSE or NULL
       ELSE ----code when your variable is TRUE
  END  -- Problematic code here.
  BEGIN
    -- Error code not shown.
  END
GO
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did you check my code. I used ISNULL function to resolve the problem.
Avatar of dportas
dportas

Sharath_123, you can't use CASE like that. CASE is not a statement.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jdana

ASKER

dportas - I appreciate the two salient points.  J