Using DBMS_XMLGen for Dynamic SQL

The DBMS_XMLGen package has some interesting functionality that allows the caller to perform SQL based on a SQL string, where previously they have had to use Dynamic SQL (execute immediate / DBMS_SQL) or compile and call PL/SQL routines.

Here’s an example, where I’m counting the number of records in a couple of tables in the HR schema.

select table_name, DBMS_XMLGen.GetXMLType('select count(*) cnt from '||table_name) XML_Data
from user_tables
where table_name in ('EMPLOYEES', 'DEPARTMENTS')
/

TABLE_NAME                       XML_DATA
------------------------------------------------------- 
'DEPARTMENTS'                    <ROWSET>
                                   <ROW>
                                    <CNT>27</CNT>
                                   </ROW>
                                 </ROWSET>

'EMPLOYEES'                      <ROWSET>
                                   <ROW>
                                    <CNT>107</CNT>
                                   </ROW>
                                 </ROWSET>

Continue reading

Advertisements

Create Structured Nested XML from Flat Data

Imagine you’ve been given the task to create an XML document which is structured according to nested level of detail, but the data source is unstructured flat data, it’s not normalised, not extracted to the relevant third normal form. It might be coming from a view, query, an external table, a spreadsheet, some legacy table etc. Anyway, you get the idea.

In other words, convert this…


        ID FIRST_NAME           LAST_NAME                   AGE CITY                 REGION                    COUNTRY
---------- -------------------- -------------------- ---------- -------------------- ------------------------- --------------------
         1 Zsazsa               Piniur                       46 Hamburg              Hamburg                   Germany
         2 Chane                Frise                        41 Düsseldorf           Nordrhein-Westfalen       Germany
         3 Brandon              Fishbourn                    41 Manchester           England                   United Kingdom
         4 Hulda                Sepey                        31 Dortmund  

-- SNIP....

Into this….

<COUNTRIES>
  <COUNTRY NAME="....">
    <REGIONS>
      <REGION NAME="....">
        <CITIES>
          <CITY NAME="....">
            <RESIDENTS>
              <RESIDENT>....

Continue reading

Nvl versus Coalesce, and the winner is…

Most Oracle developers I know use NVL, rather than COALESCE for returning the first not null value of two arguments.  Most will cite it’s for habitual, historic reasons (COALESCE was introduced later in Oracle’s history) or because it’s shorter to type.

Well NVL and COALESCE do the same thing anyway, so why would I change?

Well, there’s one very good reason why you should the SQL standards function COALESCE over Oracle’s proprietary NVL….
Continue reading

Recursive Subquery Factoring Date Bug in 11gR2

Since Oracle 11gR2, Oracle has supported Recursive Subquery Factoring – this is a more flexible way of doing recursive queries than Oracle’s Connect By. Although 11g is quite old now, it’s still being used in earnest.  Whilst writing some code recently, I noticed there appears to be a problem with them regarding dates. I found this in version 11.2.0.2.0. Let’s take a look at what I found…

with t1(a_date) as (
  select date '2018-12-26'
  from   dual
  union all
  select a_date + 1 -- Problem here
  from   t1
  where a_date + 1 < date '2018-12-30'
)
select * from t1

Error at line 5
ORA-01790: expression must have same datatype as corresponding expression

Read More…

Faster Decimal to Binary Function

There was recently a post on the Oracle forums by a person who needed to convert decimal values to a binary string. They had found the standard library PL/SQL function that you often see on Oracle sites, but its slow performance was giving him headaches. It was an interesting thread as many people offered solutions, and it showed the ingenuity of people, in coming up with alternatives. In this article I’ll show some of my efforts, and my final function which, to my pleasure, was the fastest in the thread.
Read more…