0

So this works great:

select name from users where id = @id

However, that only selects 1 guy. Say I have 3 IDs instead, normally the SQL would look something like this (without using parameters)

select name from users where id in (4,6,9)

However, it doesn't seem to work when I write

select name from users where id in (@IDs)

and insert a list into @IDs, like this

cmd.Parameters.AddWithValue("@IDs", userIDs);

Is there any way to do what I'm trying to? It's important to note that the sql I'm calling is (and has to be) a stored procedure.

5
  • Do you have control over the stored proc? Commented Nov 27, 2013 at 17:48
  • It's not made yet, so yes I do. Commented Nov 27, 2013 at 17:50
  • 1
    You can use a table value parameter (msdn.microsoft.com/en-us/library/bb675163(v=vs.110).aspx) Commented Nov 27, 2013 at 17:51
  • Then what @Hejner said. You can see an example in an answer to this question: stackoverflow.com/questions/337704/… Commented Nov 27, 2013 at 17:53
  • Create a temporary table, insert the IDs and join it to the table with the IDs. With large amounts of IDs you will hit limits. Commented Nov 27, 2013 at 18:06

3 Answers 3

2

There are two ways to do this. The first is by passing a string to a stored procedure and then adding it to a dynamic query:

-- @IDs  = '4,6,9'
DECLARE @MyQuery nvarchar(max) 
SET @MyQuery = N'SELECT name FROM users WHERE id IN (' + @IDs + ')'
EXEC(@MyQuery)

On the other hand, if you are using SQL Server 2008 or later, you can use a table-valued parameter (this is my preference).

First, create a user-defined table type:

CREATE TYPE IDList AS TABLE (
  id int
)

THEN, use the user defined type as the type for your parameter:

DECLARE @IDs IDList 
INSERT INTO @IDs (ID) VALUES (4),(6),(9)
SELECT name FROM users u INNER JOIN @IDs i WHERE u.id = i.id  

If you are using .NET to consume a stored procedure, you can find sample code for user-defined SQL types on MSDN.

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

3 Comments

if you are passing '4,6,9' wouldn't that not work since it needs to look like IN ('4','6','9')
Tsukasa - If the data type of "ID" is string, then you would want ('4', '6', '9'); but if you look at his original question, it's pretty clear the data type is int or some other numeric type, so no.
It seems like the execute query is the way to go then, sadly my boss haven't updated our sql server. It just has so many downsides that a "normal" query doesn't have.
0

You can create a dynamic SQL query inside your stored procedure:

DECLARE @SQLQuery AS NVARCHAR(500)

SET @SQLQuery = 'select name from users where id in ( ' + @userIDs + ')'

EXECUTE(@SQLQuery)

You'll have to be careful and sanitize the contents of @userIDs though to prevent SQL injection attacks.

Comments

0

On our side we are using iBatis.Net to manage this. Execute query sounds quite ugly but it still does the trick.

We were mostly using string split in SQL. See [question]How do I split a string so I can access item x?

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.