优化多次查询同一XML的sql查询

时间:2014-10-01 14:16:42

标签: sql sql-server sql-server-2005

设置@ temp1的值后,我被告知可以将这些查询优化为SELECT语句,检索2个值,而不是重复访问XML"。

DECLARE @temp1 XML
DECLARE @exec_gadget_config XML
DECLARE @exec_client_name_index NVARCHAR(500)
DECLARE @exec_client_number_index NVARCHAR(500)

SELECT @temp1 = '<gadget name="EYDocuments">                
                    <drawer id="0000000002">
                        <otherColumns>
                            <col id="000000000B" name="Client Name"  />
                            <col id="000000000C" name="Client Number"  />
                        </otherColumns>
                    </drawer>
                </gadget>'

SELECT @exec_client_number_index = (
        SELECT @temp1.value('(//drawer[@id="0000000002"]/otherColumns//col[@name="Client Name"]/@id)[1]', 'NVARCHAR(500)')
        )

SELECT @exec_client_name_index = (
        SELECT @temp1.value('(//drawer[@id="0000000002"]/otherColumns//col[@name="Client Number"]/@id)[1]', 'NVARCHAR(500)')
        )

SELECT @exec_client_number_index, @exec_client_name_index
  1. 可能吗?
  2. 我对这个问题的回应如下。这是否删除了重复访问XML&#34;?
  3. 如果没有,我该怎么办?
  4. 空间

    SELECT @exec_client_number_index = (
            SELECT @temp1.value('(//drawer[@id="0000000002"]/otherColumns//col[@name="Client Name"]/@id)[1]', 'NVARCHAR(500)')
            ), @exec_client_name_index = (
            SELECT @temp1.value('(//drawer[@id="0000000002"]/otherColumns//col[@name="Client Number"]/@id)[1]', 'NVARCHAR(500)')
            );
    

1 个答案:

答案 0 :(得分:1)

试试这个 -

DECLARE @temp1 XML
SELECT @temp1 = '<gadget name="EYDocuments">                
                            <drawer id="0000000002">
                                <otherColumns>
                                    <col id="000000000B" name="Client Name"  />
                                    <col id="000000000C" name="Client Number"  />
                                </otherColumns>
                            </drawer>
                        </gadget>'

SELECT
    a.b.value('@id', 'varchar(10)') value
FROM
    @temp1.nodes('/gadget/drawer/otherColumns/col') a(b)

<强>输出

value
----------
000000000B
000000000C