Mario
Mario

Reputation: 183

Implement an IN Query using XQuery in MSSQLServer 2005

I'm trying to query an xml column using an IN expression. I have not found a native XQuery way of doing such a query so I have tried two work-arounds:

  1. Implement the IN query as a concatenation of ORs like this:

     WHERE Data.exist('/Document/ParentKTMNode[text() = sql:variable("@Param1368320145") or 
     text() = sql:variable("@Param2043685301") or ...
    
  2. Implement the IN query with the String fn:contains(...) method like this:

    WHERE Data.exist('/Document/Field2[fn:contains(sql:variable("@Param1412022317"), .)]') = 1
    

Where the given parameter is a (long) string with the values separated by "|"

The problem is that Version 1. doesn't work for more than about 50 arguments. The server throws an out of memory exception. Version 2. works, but is very, very slow.

Has anyone a 3. idea? To phrase the problem more complete: Given a list of values, of any sql native type, select all rows whose xml column has one of the given values at a specific field in the xml.

Upvotes: 2

Views: 73

Answers (1)

Dalex
Dalex

Reputation: 3625

Try to insert all your parameters in a table and query using sql:column clause:

SELECT Mytable.Column FROM MyTable
CROSS JOIN (SELECT '@Param1' T UNION ALL SELECT '@Param2') B
WHERE Data.exist('/Document/ParentKTMNode[text() = sql:column("T") 

Upvotes: 1

Related Questions