Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing list of keys to a sproc to get recordset
Message
De
25/09/2009 07:43:48
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
23/09/2009 23:16:04
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Desktop
Divers
Thread ID:
01425870
Message ID:
01426082
Vues:
90
>Struggling with t-SQL syntax to get a set of record by passing in a string built in vb .net from a list of primary keys. Need 2 versions - one for integer keys, one for guid / uid keys
>- I can be flexible about what and how I pass the params
>I understand t-sql does not actually have arrays so my first instinct is to pass in a string with the guids delimited with ' and separated with a comma and see if the pk is in the "list" of the string.
>
>But that won't work with
>
>select * from members where cpk in (@pklist)
>
>if I leave out the delimeters and have a single string with single quotes surrounding it the above select works but only pulls the first record
>
>Integers an even bigger disaster as they a string values and the above code doesn't cast them
>
>Plan B would be to build a table from the incoming param string and join against that to get the recordset but the syntax for that is beyond my t-sql skills
>
>Have a feeling somebody has the answer to this
>
>Guidance appreciated
>
>TIA

Jeff Moden's code is the best code I have ever seen about sending and splitting a comma separated list. You can find a modified implementation on:

http://www.foxite.com/archives/sqlexec-with-a-local-cursor-0000192577.htm

It doesn't have 8000 characters limit and it does work with too many items in the list.

An alternative is to use Linq:
List<int> cpk = new List<int> {1,3,5,6,....};
cpk.Add(...);

var members = from m in db.Members
                      where cpk.Contains( m.memberId )
                      select m;
It is fast and IDs are sent as parameters. Drawback is that parameters are limited to 2100 (but that shouldn't be a problem if you can use splitting algorithms that use varchar(8000) which even wouldn't let you create 2100 IDs).

If you have SQL 2008, then of course sending a table is a nice solution.

Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform