This forum is currently locked. You can't register or post questions at this time. (read more)

How to loop through a dynamic pivot query

All posts relating to Oracle PL/SQL development.

Moderator: Tim...

Boicey
Member
Posts: 4
Joined: Thu Apr 12, 2012 7:02 am

How to loop through a dynamic pivot query

Postby Boicey » Thu Apr 12, 2012 7:10 am

Hi,

I have a dynamic pivot query I have created.
There are 3 fixed columns, and then 1..n pivot columns generated, usually 4 but anything up to 10.

I can build the dynamic sql fine, but I've just realised I can't set any static variables to use the sql in a cursor, as I don't know how many I need at design time, only run time.
I then thought I'd create a temporary global table to hold the results from the dynamic sql, and use a for loop instead to do my processing.
Again though I realised I can't do this as I don't know the table name at run time, and can't use dynamic sql in a for loop.

So basically that's as far as my ideas go and would like a suggestion as to how I can do this, or even whether I can.

I'm using ORACLE 11g

Thanks in advance

Tim...
Site Admin
Posts: 18437
Joined: Mon Nov 01, 2004 5:56 pm
Location: England, UK
Contact:

Re: How to loop through a dynamic pivot query

Postby Tim... » Thu Apr 12, 2012 8:02 am

Hi.

You have two options:

1) Always return the same number of columns. This can be done in two ways:

a) Set a maximum limit to the the number of columns and always return them, even if columns are filled with NULL.
b) Concatenate the variable number of columns into a single CSV column, giving you a fixed number of 4 columns (3 + CSV). You app can then process the CSV into its constituent parts.

2) Write some code to cope with a variable number of columns returned by a cursor. The DBMS_SQL package allows you to interrogate a cursor to find out how many columns it contains and the types of each of the columns. This way you can programatically process a variant result set. I use this type of processing for my code that produces CSV from any query.

http://www.oracle-base.com/dba/Script.p ... le=csv.sql

Cheers

Tim...
Tim...
Oracle ACE Director
Oracle ACE of the Year 2006 - Oracle Magazine Editors Choice Awards
OakTable Member
OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert
My website: http://www.oracle-base.com
My blog: http://www.oracle-base.com/blog

Boicey
Member
Posts: 4
Joined: Thu Apr 12, 2012 7:02 am

Re: How to loop through a dynamic pivot query

Postby Boicey » Thu Apr 12, 2012 8:13 am

Hi Tim,

Thanks for the reply, that is great.
The second option is definitely what I am looking for, and the output to csv bit is definitely a bonus.

Kind Regards

Tim...
Site Admin
Posts: 18437
Joined: Mon Nov 01, 2004 5:56 pm
Location: England, UK
Contact:

Re: How to loop through a dynamic pivot query

Postby Tim... » Thu Apr 12, 2012 8:36 am

Hi.

That is just an example of its usage. Another would be pushing the results of a random query to a HTML table. How do you think APEX does it? Using the same mechanism (DBMS_SQL). :)

Cheers

Tim...
Tim...
Oracle ACE Director
Oracle ACE of the Year 2006 - Oracle Magazine Editors Choice Awards
OakTable Member
OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert
My website: http://www.oracle-base.com
My blog: http://www.oracle-base.com/blog

Boicey
Member
Posts: 4
Joined: Thu Apr 12, 2012 7:02 am

Re: How to loop through a dynamic pivot query

Postby Boicey » Thu Apr 12, 2012 10:49 am

I assume I can only save a file on a server rather than locally to my PC via this method?
Been looking around for code to save a file locally, but any decent links I find all seem to be broken.

Tim...
Site Admin
Posts: 18437
Joined: Mon Nov 01, 2004 5:56 pm
Location: England, UK
Contact:

Re: How to loop through a dynamic pivot query

Postby Tim... » Thu Apr 12, 2012 12:13 pm

Hi.

Oracle only knows about the server it sits on. The exception to that is a directory object pointing to a UNC style path (//machines/share), provided no authentication is needed.

Cheers

Tim...
Tim...
Oracle ACE Director
Oracle ACE of the Year 2006 - Oracle Magazine Editors Choice Awards
OakTable Member
OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert
My website: http://www.oracle-base.com
My blog: http://www.oracle-base.com/blog

Boicey
Member
Posts: 4
Joined: Thu Apr 12, 2012 7:02 am

Re: How to loop through a dynamic pivot query

Postby Boicey » Thu Apr 12, 2012 12:59 pm

Ok thanks Tim

Tim...
Site Admin
Posts: 18437
Joined: Mon Nov 01, 2004 5:56 pm
Location: England, UK
Contact:

Re: How to loop through a dynamic pivot query

Postby Tim... » Thu Apr 12, 2012 1:06 pm

:)
Tim...
Oracle ACE Director
Oracle ACE of the Year 2006 - Oracle Magazine Editors Choice Awards
OakTable Member
OCP DBA 7.3, 8, 8i, 9i, 10g, 11g
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert
My website: http://www.oracle-base.com
My blog: http://www.oracle-base.com/blog


Return to “Oracle SQL and PL/SQL Development”

Who is online

Users browsing this forum: No registered users and 3 guests

cron