设置@ 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
空间
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)')
);
答案 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