array as parameter for IN statement

Hi,

I thought TSQL of yukon would have a way of passing an array of values to a SP which can be used by it in an IN statment.

ie:

List<int> idList = new List<int>();

idList.Add(5);
idList.Add(7);

SqlCommand cmd = new SqlCommand("sp_Core_GetDetails");
cmd.Parameters.Add("@idList", idList);
cmd.ExecuteReader();
...
SP would look like (fragment)

select id, name
from DetailsTable
where id in (@idList)
is there such a feature in yukon?

[491 byte] By [TilfriedWeissenberger] at [2008-3-3]
# 1
Array parameter for TSQL has been dropped. However, you can use TVF to format the string (array) into rows. Thus, your query would look like this:

select id, name
from DetailsTable
where id in (select * from dbo.split(@idList))

Of course you can do something similar today with sql2k. Here is a good article on the subject by Erland.

http://www.sommarskog.se/arrays-in-sql.html

oj at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 2
Nice post, but still amazing how primitive some areas are and how many inconveniences there can be when performing certain things that some of us do on a daily basis.
MarcD at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3
hi,

thanks. well, at least it's some improvement (sort of ;)!

TilfriedWeissenberger at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 4

hmmm, do you by any chance have this TVF somewhere?

I can't believe MS didn't add this TVF by default!

TilfriedWeissenberger at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 5
Here is the demo code in C# from MS:
http://msdn.microsoft.com/library/en-us/dnsql90/html/sqlclrguidance.asp?frame=true#sqlclrguidance_topic4b

using System.Collections;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
/*
* This needs to return an IEnumerable, but since an array
* does, in this case we do not need to define a new class
* that implements it: we can simply return the array.
*/
[SqlFunction(FillRowMethodName="FillRow",
TableDefinition="value nvarchar(60)")]
public static IEnumerable GetStrings(SqlString str)
{
return str.Value.Split(';');
}

/*
* This method does the decoding of the row object. Since the
* row is already a string, this method is trivial. Note that
* this method is pointed to by the annotation on the
* GetString method.
*/
public static void FillRow(object row, out string str)
{
str = (string)row;
}
}

oj at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 6
thank you kindly! Especially for the source of the code aswell!
TilfriedWeissenberger at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified