Introduction
Here, I will tell you the possible ways to return calculated Boolean values from SQL Server code. In a lot of places, you want to progress conditionally. You send some input values to the stored procedure and want to get a Boolean value which decides the future flow of the application. If you are selecting Boolean values from the table in SQL stored procedure then that is pretty simple because you are just selecting a Boolean column. But what if you do things dynamically on the go? What if that Boolean column is not part of the table? So we will see those issues here.
CASE 1 - When Boolean Column is already there in the table and we need to return the same from the stored procedure
- CREATE TABLE [dbo].[Students](
- [StudentId] [int] NOT NULL,
- [StudentName] [nvarchar](50) NOT NULL,
- [CourseName] [nvarchar](50) NOT NULL,
- [IsEnrolled] [bit] NOT NULL
- )
Lets check what is in the table.
- SELECT * FROM [dbo].[Students]
Output comes as given below,
So, the stored procedure will look like this.
- CREATE PROCEDURE [dbo].[usp_IsEnrolled]
-
-
- @StudentId INT
- AS
- BEGIN
- Select IsEnrolled from dbo.Students Where StudentId=@StudentId
- END
- GO
The result of this stored procedures, when running with parameter @StudentId = 2, comes like this.
So, this was pretty easy for you as the column you are selecting is itself boolean.
Understanding
In the procedure written above, we are trying to find out whether the student is enrolled or not. So as we tried to check for Rahul it gives the value as 0 equivalent to False. So the field which we are selecting is itself a BIT hence it becomes very easy to get the boolean value in this manner.
CASE 2 - Let us return a calculated boolean from SQL stored procedure based on some condition.
Let us check if the student is from B.Tech course by the following procedure.
- CREATE PROCEDURE [dbo].[usp_IsBTechCandidate]
-
-
- @StudentId INT
- AS
- BEGIN
- DECLARE @IsBTech BIT
- IF EXISTS(SELECT * FROM dbo.Students Where StudentId=@StudentId and CourseName='B.Tech')
- BEGIN
- SET @IsBTech=1
- END
- ELSE
- BEGIN
- SET @IsBTech=0
- END
- SELECT @IsBTech AS 'IsBTech'
- END
This procedure, when executed with StudentId=1, gives the following result with return type Boolean only.
Understanding
This case is a little different from above in the sense that we do not actually have the property stored in the table which we are looking for. So, here we are actually calculating the BIT Value. We are trying to find out whether the student is enrolled for B.Tech course. Hence when we executed CASE 2 procedure with parameter StudentId = 1 for student Kamal in the table we get a True return value. This is also directly coming as BIT because we have declared it as a BIT and then based on the condition we are assigning a value.
CASE 3 - Achieving whatever done in case 2 without declaring a bit variable
So, we can rewrite the above procedure as below.
- CREATE PROCEDURE [dbo].[usp_IsBTechCandidate]
-
-
- @StudentId INT
- AS
- BEGIN
- IF EXISTS(SELECT * FROM dbo.Students Where StudentId=@StudentId and CourseName='B.Tech')
- BEGIN
- SELECT CAST(1 AS BIT) AS 'IsBTech'
- END
- ELSE
- BEGIN
- SELECT CAST(0 AS BIT) AS 'IsBTech'
- END
- END
Here, we have used CAST function to return a bit. Based on this value we can take the flow of our application further. Output of the above procedure comes as below when StudentId=1
Understanding
In case 3, we have the same problem statement like we are trying to find out whether the student is a B.Tech candidate or not. But the implementation is a bit different -- here we have not declared a BIT variable. We need to directly return the calculated BIT value based on condition. So we use the CAST() method available in SQL. We can use the CAST method to cast from one data type to another. You might want to cast a float value to int value for that we use CAST(). So in case three is based on the condition we are casting int 1 to BIT which returns True value and int 0 to BIT which returns False value.
Hence there are multiple ways by which we can return Boolean values from stored procedures. The main ways we have described above.
Summary
So, you have seen above that if you have declared a variable in BIT datatype in stored procedure declaration, then you will, by default, get its value in Boolean(C#) / BIT data type but if you are not using BIT variable, then you will have to cast that int value to the BIT data type as seen in CASE 3. In this process, anything which is non zero is assumed TRUE. So, for values like -2, -1, 1, 2 and so when you will get true value and for 0, you will get False.
In this way, we can implement the scenario using any of CASE 1, CASE 2 and CASE 3 methodologies based on our need. Write in the comments if you have some issues while performing these operations.