DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
<Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
<OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
<OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
</Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
<OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
</Order>
</Customer>
</ROOT>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer',1)
WITH (CustomerID varchar(10),
ContactName varchar(20))下面是结果集: CustomerID ContactName
---------- --------------------
VINET Paul Henriot
LILAS Carlos Gonzlez
这种文档早看过了。缺陷是1.没看到怎么带查询条件查,比如emplyeeid >5 2.openxml占内存太大,不考虑用
3q all the same
那在node里不是一样吗?.或者;WITH LiangAndLan AS
(
SELECT
x.item.value('@topid[1]','int') as topid,
x.item.value('@photoid[1]','int') as photoid,
x.item.value('@viewcount[1]','int') as viewcount
FROM @temp.nodes('/TopList/Photos') as x(item)
)
SELECT
xx,xx,xxx
FROM LiangAndLan
WHERE 你的条件.
(
SELECT x.item.value('@topid[1]','int') as topid,
x.item.value('@photoid[1]','int') as photoid,
x.item.value('@viewcount[1]','int') as viewcount
FROM @temp.nodes('/TopList/Photos') as x(item)
) A
WHERE topid>1
query里用到了'TopList/Photos[@topid>1]'
是把符合条件的node调出来
你用 SELECT
x.item.value('@topid[1]','int') as topid,
x.item.value('@photoid[1]','int') as photoid,
x.item.value('@viewcount[1]','int') as viewcount
FROM @temp.nodes('/TopList/Photos') as x(item) 必然把XML的所有节点提出来塞在临时表里面,就好像你只要符合要求的10条记录提取取出来,但是你得朝临时表里先插1000条记录再来个取10条,明显很低效了