Sql Server: Using CASE Statement inside IN Clause (alternative)

Introduction:  In this article I am going to share best alternative of using CASE statement inside IN operator in sql server with suitable examples.

Using CASE Statement inside IN Clause in sql server (alternative)


Description: While working with sql server database I got the following requirement.
I was to get the permissions according to user type. i.e.

  • If UserType=1 then permission will be Read & Write, Update and Delete.
  • If UserType=2 then permission will be Read & Write, Update.
  • If UserType=3 then permission will be Read Only.

Here UserType=1:Admin,  2: User,  3: Guest User

Implementation: Let’s create PermissionType table and insert data in it using following script.

CREATE TABLE tbPermissionType
(
            PermissionId               INT IDENTITY(1,1) PRIMARY KEY,
            PermissonType            VARCHAR(100)          
)

INSERT INTO tbPermissionType VALUES('Read Only')
INSERT INTO tbPermissionType VALUES('Read & Write')
INSERT INTO tbPermissionType VALUES('Update')
INSERT INTO tbPermissionType VALUES('Delete')

Check table data
SELECT * FROM tbPermissionType

PermissionId
PermissionType
1
Read Only
2
Read & Write
3
Update
4
Delete

To get what I need I first I tried using CASE inside IN Clause that first came up in my mind.

DECLARE @UserType TINYINT=1;
SELECT PermissonType FROM tbPermissionType
WHERE  PermissionId IN (CASE WHEN @UserType=1 THEN (2,3,4)
                                                   WHEN @UserType=2 THEN (2,3)
                                                   WHEN @UserType=3 THEN (1) END
                                          )
But I got the following error:
Incorrect syntax near ','.

Then I searched on google and got many ways to get this done. I tried many ways and found the result I need. But then I tried much efficient and clear way without using CASE statement that I am going to share.
Let’s try by passing 1, 2, 3 value to the UserType variable and see whether we get the desired result or not.

Execute the following by passing UserType=1:
DECLARE @UserType TINYINT=1;
SELECT PermissonType FROM tbPermissionType
WHERE
(@UserType = 1 AND PermissionId IN (2,3,4))        
OR
(@UserType =2 AND PermissionId IN (2,3))
OR
(@UserType =3 AND PermissionId=1)

Result will be as expected.

PermissionType
Read & Write
Update
Delete

Now execute the following by passing UserType=2

DECLARE @UserType TINYINT=2;
SELECT PermissonType FROM tbPermissionType
WHERE
(@UserType = 1 AND PermissionId IN (2,3,4))        
OR
(@UserType =2 AND PermissionId IN (2,3))
OR
(@UserType =3 AND PermissionId=1)

Result will be as expected.
PermissionType
Read & Write
Update

Now execute the following by passing UserType=3
DECLARE @UserType TINYINT=3;
SELECT PermissonType FROM tbPermissionType
WHERE
(@UserType = 1 AND PermissionId IN (2,3,4))        
OR
(@UserType =2 AND PermissionId IN (2,3))
OR
(@UserType =3 AND PermissionId=1)

Result will be as expected.
PermissionType
Read Only

So it is clear  from the result that without using CASE Statement we can get the result in efficient way.

 Now over to you:
A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linkedin and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates. 
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..