Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Equivalent of VFP INLIST with string of values
Message
From
27/11/2007 19:34:55
 
 
To
27/11/2007 18:56:31
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01271536
Message ID:
01271614
Views:
21
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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform