English 中文(简体)
SQL OpenXML returns nothing when the XML doesn t fill every level
原标题:

I m having an issue with OPENXML in SQL Server 2005 where I ll get no results back if the XML doesn t have every tier available. An example would clear up my question:

So we have XML like this:

<Connection Name="DEFAULT" /> 
    <Branch Name="A_Branch">
        <Leaf Name="A_Leaf.OP" >
             <Property Name="A_Property" />
        </Leaf>
    </Branch>
</Connection>

And the OPENXML puts into into a table variable like this

INSERT INTO @xmlDataTable
    SELECT *
    FROM OPENXML(@idoc,  /Connection/Branch/Leaf , 2)
        WITH (
              Connection varchar(100)  ../../@Name 
            , Branch varchar(100)  ../@Name 
            , Leaf varchar(100)  @Name 
            )

And that works fine! But if you put this XML in:

<Connection Name="DEFAULT">
</Connection>

Then the OPENXML returns nothing, an empty row set.

So I m really not sure what to do to fix that. I need to account for both scenarios, and the scenario with Branches but no leaves. Any thoughts?

最佳回答

If you ask for /Connection/Branch/Leaf then you re going to get exactly what you ask for. The XML snipped you show must return an empty row set, anything else would e incorrect. If you want <connection name="DEFAULT"/> to return something, then ask for /Connection. If you want <connection name="DEFAULT"><branch name="A_Branch"/></connection> to return something, then ask for /Connection/Branch. And so on and so forth.

You can t expect a database to process a query in a manner like I couldn t find the row you asked for, but here is something else you may find interesting... .

If you want /Connection/Branch/Leaf and /Connection/Branch and /Connection then ask for that, use multiple queries and UNION.

问题回答

暂无回答




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签