Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to pass parameterin stored Procedure as like
'abc','xyz','cde','efg'


create procedure PrcName
@anyID varchar(max)

as
begin

SELECT *
FROM mytable
WHERE mytable.id IN (@anyID)
end

What I have tried:

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
Posted
Updated 21-Jun-19 1:00am

 
Share this answer
 
What you are seeking is the String_Split function in SQL Server. This is a table valued function that will return rows from your input string.
SQL
CREATE PROCEDURE PrcName (
  @anyID varchar(max)
) AS
BEGIN
  SELECT *
  FROM   mytable
  WHERE  mytable.id IN (
    SELECT value
    FROM   String_Split(@anyID, ' ')
  )
END

This function is native as of SQL Server 2016. For older versions, there are numerous variants of this function available for you to create.

Reference: STRING_SPLIT (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
Comments
Anuragintit 21-Jun-19 8:41am    
Thanks, works fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900