Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Parsing Oracle PIVOT XML data

by tweetiepooh (Hermit)
on Jan 18, 2016 at 09:27 UTC ( #1153003=note: print w/replies, xml ) Need Help??


in reply to Re: Parsing Oracle PIVOT XML data
in thread Parsing Oracle PIVOT XML data

The Perl side of this is pretty simple

# connect to the database and set date format # for these reports dates are likey truncated somehome my $dbh = connectdb(); $dbh->do("alter session set nls_date_format='dd Month yyyy hh24:mi:ss' +"); # uncomment the following for long running reports $dbh->do("alter session disable parallel query"); $dbh->do(qq(alter session set "_b_tree_bitmap_plans"=false)); $dbh->do("alter session set db_file_multiblock_read_count=2048"); # compile the SQL my $sth = $dbh->prepare(SQL); # run the query $sth->execute(); # retrieve the names of the columns returned and write header line my @names = @{$sth->{NAME}}; # create a hash based on the first column my %data = %{$sth->fetchall_hashref($names[0])}; print Dumper(%data);

What I'd essentially expect is 2 strings, one the key and the second the XML. I'd then parse the XML to get my columns and fill the data in. The intention is to have this as a simple template so anyone can copy the file, edit the SQL (just changing limits, and data items) and the report would generate what's needed. I already have this for basic list reports.

The issue seems that somewhere in the data line the XML shown as a string in SQLPlus is getting converted somehow.

Replies are listed 'Best First'.
Re^3: Parsing Oracle PIVOT XML data
by poj (Abbot) on Jan 18, 2016 at 17:03 UTC

    A work around might be to use a temporary table

    my $sql = ' insert into TMP_XML value ( select * from ( select f1,f2,t from table where date > sysdate - 2 ) pivot xml ( count(t) as al, sum(t) as ev for f2 in (any) ) )'; $dbh->do($sql); my $sth = $dbh->prepare('SELECT * FROM TMP_XML'); $sth->execute();
    poj

      Thanks. That will work as a workaround and possibly a final solution. One issue I would have is that the account used to create reports can't create tables. I can get round it but it lacks some of the flexibility with lots of reports running at same time.

      It's just interesting as to what is getting returned parsed by DBI or DBD::Oracle from the original. I can't use the SQL SELECT to create a table so it's not returning a "simple" value.

        I would assume that creating temporary tables is possible even if the user lacks the permissions to create (permanent) tables:

        CREATE GLOBAL TEMPORARY TABLE my_temp_table (...) ON COMMIT DELETE ROWS;

        As to the larger issue, I think it's mainly an issue of the DBD to return the structured data from the XML query, and maybe supporting that isn't that easy without the driver becoming "locked" to a specific version of Oracle.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1153003]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2022-11-28 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?