1

I have the following ms-sql query

select * from category where categoryId in (1)

Now instead of 1, I need to pass more than one categoryId dynamically using parameters. Something like this:

select * from category where categoryId in (@catId)

and value of @catId would be something like '1,2'. When I tried this I got an error saying

Error converting data type varchar to bigint.

How can I pass more than one categoryId.

Thanks in advance.

2 Answers 2

2

You can use Table-valued parameters, a new feature in SQL Server 2008

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the XML data type instead to pass a list of ID's.

declare @catId xml = '<i>1</i><i>2</i><i>3</i>'

select *
from category
where categoryid in (select T.N.value('.', 'int')
                     from @catId.nodes('/i') as T(N))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.