Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Equivalent of VFP INLIST with string of values
Message
De
27/11/2007 19:34:55
 
 
À
27/11/2007 18:56:31
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01271536
Message ID:
01271614
Vues:
20
Haha! Yeah, Kind of Blue always calms me down.

Thanks for your comments.

In a nutshell, the technique I describe in my article may come close to what you're looking for. Here's the Reader's Digest version of it:


First, create a table-valued UDF in your database, as such:
CREATE FUNCTION [dbo].[XML2TableVar] 
(  @XMLString XML) 
RETURNS 
@tPKList TABLE  ( PKValue int) 
AS 
BEGIN 

            INSERT INTO @tPKList 
              SELECT Tbl.Col.value('.','int') 
                  FROM   @XMLString.nodes('//PKValue') Tbl( Col ) 
   RETURN 
END 
This will allow you to pass any XML string and receive a table variable in return.

So you can use it in query analyzer like the following:
DECLARE @XMLString XML 
SET @XMLString ='<Customers> 
                      <Customer> 
                           <PKValue>2</PKValue> 
                           <PKValue>5</PKValue> 
                      </Customer> 
                 </Customers>' 


-- Perform a JOIN using the UDF 

SELECT Customers.* FROM Customers 
                       JOIN [dbo].[XML2TableVar] (@XMLString) CustList 
                                    ON Customers.CustomerPK = CustList.PKValue
So there it is...you just call the UDF in your SELECT statement, and it returns a table variable that you can use in a JOIN.

Pretty re-usable....now, the one requirement is that your selections in the XML must use the column name PKVALUE. If you want to make it variable, you'd have to modify the UDF.

Let me know what you think....again, thanks for your comments, I appreciate them.

Kevin
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform