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.


About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at www.sqlteam.com and a moderator at www.sql-server-performance.com. His T-sql blog is at http://beyondrelational.com/blogs/madhivanan

No comments: