{"id":330,"date":"2017-02-20T09:55:39","date_gmt":"2017-02-20T13:55:39","guid":{"rendered":"http:\/\/dbtricks.com\/?p=330"},"modified":"2017-02-20T10:06:23","modified_gmt":"2017-02-20T14:06:23","slug":"run-a-function-with-a-table-of-user-defined-type-udt-as-a-parameter-using-sqlplus","status":"publish","type":"post","link":"https:\/\/dbtricks.com\/?p=330","title":{"rendered":"Run a function with  a table of User Defined Type (UDT)  as a parameter using SQLPlus"},"content":{"rendered":"<p>If you want to send  multiple records to an oracle function, a common practice is to create the function with one or more parameters as a table of an object type. <\/p>\n<p>In the function itself, you can loop thru the array or select from it using to TABLE command. <\/p>\n<p>Another option to send an array of multiple records  is to use XML (xmltype) as the function parameter but this will be addressed in a separate post.<\/p>\n<p>While I use this method for years, for some reason, many times I find myself struggling with the relatively simple syntax. This post will serve me (and maybe some of you) as a short, very simplistic  template to copy from. <\/p>\n<p>Basically, what you need to do is the create a user defined object type (UDT)  <\/p>\n<pre lang=\"sql\">\r\ncreate or replace type order_type as object (\r\n  num_of_units number\r\n, cost_of_unit number)\r\n<\/pre>\n<p>Then you create a table of this user defined type<\/p>\n<pre lang=\"sql\">\r\ncreate or replace type order_tbl as  table of order_type\r\n<\/pre>\n<p>A simple function<\/p>\n<pre lang=\"sql\">\r\ncreate or replace function calc_total (i_orders order_tbl) return number is\r\n\r\nres number;\r\n\r\nbegin\r\n\r\n\r\n   select sum(num_of_units * cost_of_unit)\r\n   into res\r\n   from table (i_orders);\r\n\r\n  \r\n\r\n   return res;\r\n\r\nend;\r\n<\/pre>\n<p>Testing this function with SQLPlus is a little challenging.   The simple way to send a table of UDT type to a function is to cast it to the table of the user defined type.<\/p>\n<pre lang=\"sql\">\r\nSQL>   select  calc_total(\r\n  2                      cast(MULTISET(\r\n  3                        select * from\r\n  4                        (\r\n  5                        select 9 num_of_units, 2 cost_of_unit from dual union all\r\n  6                        select 6, 3  from dual\r\n  7                        ) ) as order_tbl   ))\r\n  8    from dual;\r\n \r\nCALC_TOTAL(CAST(MULTISET(SELEC\r\n------------------------------\r\n                            36\r\n\r\n<\/pre>\n<p>The reason for the additional inline view is the weird oracle bug the cause the <a href=\"http:\/\/dbtricks.com\/?p=229\" title=\"Using Union together with Cast and Multiset can cause your function to simply hang\">function to hang (and sometimes crash with  ora-03113 and  ora-00600 ) when using cast and multiset together with union<\/a>.  The inline view is fast workaround for this.<br \/>\nAnother option to work around this problem is to use multiset union <\/p>\n<pre lang=\"sql\">\r\nSQL>    select  calc_total( (cast(multiset(\r\n  2                               select 9,2 from dual\r\n  3                                MULTISET UNION\r\n  4                               select 6,3 from dual\r\n  5                           ) as order_tbl   ))\r\n  6                       )\r\n  7     from dual;\r\n \r\nCALC_TOTAL((CAST(MULTISET(SELE\r\n------------------------------\r\n                            36\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you want to send multiple records to an oracle function, a common practice is to create the function with one or more parameters as a table of an object type. In the function itself, you can loop thru the array or select from it using to TABLE command. Another option to send an array [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[106,96,13,95,105],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-oracle","tag-cast","tag-multiset","tag-ora-00600","tag-ora-03113","tag-udt"],"_links":{"self":[{"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dbtricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":13,"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/dbtricks.com\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions\/356"}],"wp:attachment":[{"href":"https:\/\/dbtricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbtricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbtricks.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}