Passing multiple value to IN Operator

Rajnilari2015
Posted by Rajnilari2015 under Sql Server category on | Points: 40 | Views : 540
The below code will demonstrate how to pass multiple value in a variable which is used with IN operator

CREATE TABLE #T(Region VARCHAR(100))
INSERT INTO #T SELECT 'EAST' UNION ALL SELECT 'WEST' UNION ALL SELECT 'NORTH' UNION ALL SELECT 'SOUTH'

DECLARE @sql NVARCHAR(1000)
DECLARE @RegionName VARCHAR(100) = 'EAST,WEST'
DECLARE @Region VARCHAR(100) = '''' + Replace(@RegionName,',',''',''') +''''

SET @sql = 'SELECT
t.*
FROM #T t
WHERE t.Region IN(' + @Region + ')'

EXECUTE sp_executesql @sql
DROP TABLE #T


Region
--------
EAST
WEST

Comments or Responses

Login to post response