I collected some values to be looked up from a DB column inside a string variable and was trying to pass this as a parameter in the SQL StoredProcedure.
ALTER PROCEDURE [dbo].[InkDB]
(
@ser nvarchar(255),
@svt nvarchar(255)
)
AS
SELECT DISTINCT Details from tbData WHERE (Name IN @svt AND Address=@ser)
This gives me a syntax error near @svt message while trying to run the query.
From my webpage, the parameter has value something like ('PersonA', 'Person B', 'Person C') that is being passed. How do I use the IN statement in this case?
Its a common mistake – you are passing a single value (expression) of type string to
INoperator butINexpects a comma delimited list of values (expressions) and not a single string variable.What you need to do here is to have a function that would split the given parameter into a multiple values based on given delimiter and then use that list with
INkeyword. For example,where
efn_Splitis a table value function that will split comma-separated values into a table. See these various SO questions for implementation of such function:Split function equivalent in T-SQL?
How to split string using delimiter char using T-SQL?
Yet another alternative is to construct the SQL statement and execute with
sp_executesql.