I have a myStoreProcedure
like
@personId VARCHAR(MAX)
SELECT IdNo, LastName + ', ' + FirstName + ' ' + MiddleName AS Name
FROM Person
WHERE CAST(Person.PersonId AS varchar(max)) in (@personId)
I want to list all the persons based on PersonId
by using the WHERE IN
but the datatype is varchar
. So far this is what I have tried.
var p0 = p.Select(x => new { id = "'" + x.ToString() + "'" })
.Select(c => c.id).ToArray();
string personIds1 = string.Join(",", p0);
Let say, I have 2, 5
PersonId's, so by joining the Id
with the code above the result is
personIds1 = '2','5'
Now, when I call the myStoreProcedure
with the parameter I got no results.
var list = myEntities.myStoreProcedure(personIds1).Select(t => new PersonEntity
{
IdNo = t.IdNo,
Name = t.Name,
}).ToList();
My question is:
How to create a stored procedure to read the varchar
just like the example:
--@personId = "'2', '5'" <-just an example
WHERE CAST(Person.PersonId AS varchar(max)) in (@personId)
so I can get the PersonId
2, 5 and so on.