Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing list of keys to a sproc to get recordset
Message
De
30/09/2009 11:14:09
 
 
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:
01426905
Vues:
49
Thanks again. I find all this very valuable as I am a t-sql novice (though I've worked with it for 10 years <s> ) and any code I see extends my knowledge.

Since I'm only using SQL2008, I've found that table valued parameters are just what I need for all kinds of things. I've built stuff into my BO base class now to handle datatables of keys passed into sprocs and it is a beautiful thing. I haven't got to doing inserts that way yet but I can definitely see times when that - as well as certain kinds of joins - will be very useful. Definitely a major major reason to upgrade to 2008+ if possible.

>Hi Charles,
>
>Here is an interesting and fast variation of the split function
>
>CREATE FUNCTION  [dbo].[Split](
>    @List VARCHAR(8000),
>    @Del VARCHAR(1)
>) RETURNS TABLE
>AS
>RETURN (
>    WITH Tokens AS (
>        SELECT
>            StartPos = 1,
>            EndPos = CHARINDEX(@Del, @List COLLATE Latin1_General_BIN2 +@del)
>        UNION ALL
>        SELECT
>            EndPos + 1,
>            CHARINDEX(@Del, @List COLLATE Latin1_General_BIN2 + @del, EndPos + 1)
>        FROM Tokens
>        WHERE EndPos > 0
>    )
>    SELECT
>        SUBSTRING(@List, StartPos, EndPos - StartPos) as value
>    FROM Tokens
>    WHERE EndPos <> 0
>)    
>
>
>See http://forums.asp.net/p/1476140/3433391.aspx#3433391
>
>
>>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


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform