Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 OPENXML vs. CROSS APPLY (I Guess XQUERY)

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-01 : 15:08:54
Someone just showed me CROSS APPLY to shred XML

I have been very happy with OPENXML as it appears to be straight forward.

In OPENXML I do something like


DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<ROOT>
<Customer CustomerID="VINET" >
<ContactName>Paul Henriot</ContactName>
<Order OrderID="10248" 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</ContactName>
<Order OrderID="10283" 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 using OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer',3)
WITH (CustomerID varchar(10),
ContactName varchar(20))

SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer/Order',3)
WITH (OrderID varchar(10),
CustomerID varchar(20),
EmployeeID varchar(10),
OrderDate datetime)
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',3)
WITH ( OrderID varchar(10),
ProductID varchar(10),
Quantity varchar(20))


EXEC sp_xml_removedocument @idoc


In CROS APPLY I see stuff Like (now it may be written poorly, but I wouldn't know):


select x.y.value('res_cd[1]', 'int')
--,x.y.value('res_version[1]','int')
,@res_version
,x.y.value('cat_cd[1]','CHAR(3)')
,x.y.value('org_cd[1]','VARCHAR(20)')
,x.y.value('res_name[1]','VARCHAR(50)')

-- begin null fields
,case when x.y.value('func_role_cd[1]','int') = -1
then Null
else
x.y.value('func_role_cd[1]','int')
end
,case when x.y.value('func_role_version[1]','int') = -1
then Null
else
x.y.value('func_role_version[1]','int')
end

,case when x.y.value('approval_ind[1]','char(01)') = ' '
then Null
else
x.y.value('approval_ind[1]','char(01)')
end
,case when x.y.value('approval_dt[1]','datetime') = '1900-01-01 00:00:00.000'
then Null
else
x.y.value('approval_dt[1]','datetime')
end
,case when x.y.value('create_dt[1]','datetime') = '1900-01-01 00:00:00.000'
then Null
else
x.y.value('create_dt[1]','datetime')
end
,case when x.y.value('res_eff[1]','datetime') = '1900-01-01 00:00:00.000'
then Null
else
x.y.value('res_eff[1]','datetime')
end
,case when x.y.value('res_term[1]','datetime') = '1900-01-01 00:00:00.000'
then Null
else
x.y.value('res_term[1]','datetime')
end
,case when x.y.value('res_desc[1]','varchar(255)') in ('NULL','Null',' ')
then Null
else
x.y.value('res_desc[1]','varchar(255)')
end
,case when x.y.value('opt_ind[1]','char(01)') = ' '
then Null
else
x.y.value('opt_ind[1]','char(01)')
end
,case when x.y.value('comments[1]','varchar(255)') in ('NULL','Null',' ')
then Null
else
x.y.value('comments[1]','varchar(255)')
end
,case when x.y.value('status[1]','varchar(20)') in ('NULL','Null',' ')
then Null
else
x.y.value('status[1]','varchar(20)')
end
,case when x.y.value('attachment_allowed_ind[1]','char(01)') = ' '
then Null
else
x.y.value('attachment_allowed_ind[1]','char(01)')
end
,case when x.y.value('attachment_ind[1]','char(01)') = ' '
then Null
else
x.y.value('attachment_ind[1]','char(01)')
end
,x.y.value('add_id[1]','char(12)')
-- ,x.y.value('add_ts[1]','datetime')
,getdate()
,x.y.value('update_id[1]','char(12)')
--,x.y.value('update_ts[1]','datetime')
,getdate()

from #temp_xmldata
cross apply data.nodes('update_resource/resource') x(y)



What are the [1]'s for??? or the x.y.value??

Is this even T-SQL?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


X002548
Not Just a Number

15586 Posts

Posted - 2011-03-01 : 15:22:21
aha...XQUERY

reading...reading....

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-01 : 16:19:57
quote:
aha...XQUERY

reading...reading....
Yeah, stick with it. It's a pain to understand at first, but really nice once you get it.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-02 : 12:11:23
Sure seems like OPENXML is much simpler

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-02 : 13:42:24
quote:

XQuery is About Querying XML

XQuery is a language for finding and extracting elements and attributes from XML documents.



Is this Overkill if I just want to shred a document?




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -