SQL Server: Return Boolean from Stored Procedure

Sometimes you may need to return a Boolean value from a SQL Server stored procedure. This article explains how to do so.

Create a sample procedure with an output parameter of the ‘bit’ datatype. The supported datatype of boolean in SQL Server is bit.

storedprocboolean

Here's the same code for you to try out:

CREATE PROCEDURE test
(
@date datetime,
@status bit output
)
AS
IF @date<GETDATE()
SET @status=1
ELSE
SET @status=0
GO

The above stored procedure will return 1 if the date value passed to the parameter is less than the current date and time, otherwise it will return 0.


DECLARE @date date, @b bit
SET @date='20081101'
EXEC test @date,@b output

SELECT @b

GO

OUTPUT

boolean4

DECLARE @date date, @b bit
SET @date='20110419'
EXEC test @date,@b output

SELECT @b

boolean5

The first code returns 1 and second code returns 0. @bit is of type bit and it receives output from the stored procedure.

No comments:

Post a Comment