{"id":1328,"date":"2012-06-04T09:30:11","date_gmt":"2012-06-04T03:30:11","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1328"},"modified":"2016-01-07T11:31:04","modified_gmt":"2016-01-07T05:31:04","slug":"retrieving-data-as-xml-using-stored-procedure","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1328","title":{"rendered":"Retrieving data as XML using stored procedure"},"content":{"rendered":"<p>On a <a href=\"https:\/\/techsatwork.com\/blog\/?p=1318\" target=\"_blank\">previous post<\/a> I showed how to insert parent-child data from an XML document using stored procedure. Well here I am going to show how to retrieve data from a table as an XML document. \u00c2\u00a0Using the table set of tables from the previous post lets retrieve all the order details for a given customer. \u00c2\u00a0Here is a stored procedure that will take customer_id as an input and then return an xml document that has all the orders and order item that customer has purchased (customer order history) :<\/p>\n<p><span style=\"color: #ff9900;\">CREATE PROCEDURE spRcustomer_order_history<\/span><br \/>\n<span style=\"color: #ff9900;\">(IN customerid int, OUT customerorders XML)<\/span><br \/>\n<span style=\"color: #ff9900;\">DYNAMIC RESULT SETS 1<\/span><\/p>\n<p><span style=\"color: #ff9900;\">P1: BEGIN <\/span><br \/>\n<span style=\"color: #ff9900;\">declare not_found condition for &#8216;02000&#8217;;<\/span><br \/>\n<span style=\"color: #ff9900;\">declare exit handler for not_found <\/span><br \/>\n<span style=\"color: #ff9900;\">\u00c2\u00a0 \u00c2\u00a0set customerorders = xmlcast(&#8216;NO ROWS FOUND&#8217; as xml);<\/span><\/p>\n<p><span style=\"color: #ff9900;\">#####################################################<\/span><br \/>\n<span style=\"color: #ff9900;\"> &#8212; # Returns results in a XML parameter<\/span><br \/>\n<span style=\"color: #ff9900;\">#####################################################<\/span><\/p>\n<p><span style=\"color: #ff9900;\">select xmlelement(name &#8220;customerid&#8221; , xmlattributes (o.customer_id),<\/span><br \/>\n<span style=\"color: #ff9900;\"> xmlagg(<\/span><br \/>\n<span style=\"color: #ff9900;\"> xmlelement(name &#8220;order_id&#8221;,o.order_id,<\/span><br \/>\n<span style=\"color: #ff9900;\"> xmlelement(name &#8220;order_timestamp&#8221;,o.order_timestamp),<\/span><br \/>\n<span style=\"color: #ff9900;\"> ( select <\/span><br \/>\n<span style=\"color: #ff9900;\"> xmlagg( <\/span><br \/>\n<span style=\"color: #ff9900;\"> xmlelement(name &#8220;product_id&#8221;, oi.product_id, <\/span><br \/>\n<span style=\"color: #ff9900;\"> (xmlforest (oi.qty as &#8220;qty&#8221;,<\/span><br \/>\n<span style=\"color: #ff9900;\"> oi.unit_price as &#8220;unit_price&#8221;,<\/span><br \/>\n<span style=\"color: #ff9900;\"> oi.product_discount as &#8220;product_discount&#8221;,<\/span><br \/>\n<span style=\"color: #ff9900;\"> (oi.qty*oi.unit_price-oi.product_discount) as &#8220;total_price&#8221;<\/span><br \/>\n<span style=\"color: #ff9900;\"> )))) from order_items oi<\/span><br \/>\n<span style=\"color: #ff9900;\"> where o.order_id = oi.order_id<\/span><br \/>\n<span style=\"color: #ff9900;\"> )))) into customerorders<\/span><br \/>\n<span style=\"color: #ff9900;\">FROM orders o<\/span><br \/>\n<span style=\"color: #ff9900;\">where o.customer_id = customerid<\/span><br \/>\n<span style=\"color: #ff9900;\"> group by customer_id<\/span><br \/>\n<span style=\"color: #ff9900;\"> ; <\/span><br \/>\n<span style=\"color: #ff9900;\">END P1<\/span><br \/>\n<span style=\"color: #ff9900;\">@<\/span><\/p>\n<p>Now lets call the stored procedure:<br \/>\n<span style=\"color: #ff9900;\">Call spRcustomer_order_history(13453,?);<\/span><\/p>\n<p><span style=\"color: #99cc00;\">&lt;customerid CUSTOMER_ID=&#8221;13453&#8243;&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;order_id&gt;1003<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;order_timestamp&gt;2012-05-24T21:05:33.552802&lt;\/order_timestamp&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;product_id&gt;Z001<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;qty&gt;10&lt;\/qty&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;unit_price&gt;10.50&lt;\/unit_price&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;product_discount&gt;.00&lt;\/product_discount&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;total_price&gt;105.00&lt;\/total_price&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;\/product_id&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;product_id&gt;Z002<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;qty&gt;14&lt;\/qty&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;unit_price&gt;20.50&lt;\/unit_price&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;product_discount&gt;.00&lt;\/product_discount&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;total_price&gt;287.00&lt;\/total_price&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;\/product_id&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;\/order_id&gt;<\/span><br \/>\n<span style=\"color: #99cc00;\">&lt;\/customerid&gt;<\/span><\/p>\n<p>This will retrieve order history for the customer 13453 and return the data in XML format. \u00c2\u00a0Since the data is returned in an XML format, you do not need to use a cursor. \u00c2\u00a0Test it out and me know if there is any errors or a better way to do it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a previous post I showed how to insert parent-child data from an XML document using stored procedure. Well here I am going to show how to retrieve data from a table as an XML document. \u00c2\u00a0Using the table set of tables from the previous post lets retrieve all the order details for a given [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[303,3,358],"tags":[799,802,825,800,826,823,746,487,824,801,822,818,821,820,819,488],"class_list":["post-1328","post","type-post","status-publish","format-standard","hentry","category-database","category-db2","category-how-to","tag-db2-stored-procedure","tag-db2-xml","tag-getting-data-from-db2-in-xml","tag-purexml","tag-querying-db2-in-xml","tag-retrieve-data-in-xml","tag-stored-procedure","tag-xml","tag-xml-output","tag-xml-query","tag-xml-stored-procedure","tag-xmlagg","tag-xmlattributes","tag-xmlelement","tag-xmlforest","tag-xquery"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1328"}],"version-history":[{"count":2,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1328\/revisions"}],"predecessor-version":[{"id":1331,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1328\/revisions\/1331"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}