Finding max column length returned from Perl DBI Oracle query

Posted on 2011-03-09
Last Modified: 2012-05-11
I'm using Perl DBI to access an Oracle database.

I'm trying to get the actual maximum size of the columns returned from a query, so I can better format the output. I'm working with user-entered queries, so I don't have the luxury of knowing tables and columns in advance. I know that this:

Open in new window

will return the declared length of a field, but not the max size from the answer set.

This query:
select max(length(<col>)) from <table>

Open in new window

returns the largest entry for a given column which is close to what I need. I'm working with user-entered data, though, and I'm not sure how to get the tables and columns, to use in the above query.

Has anyone had success doing this? My last resort is to load the entire answer set in an array and find the max size by brute force, but that will be really inefficient with a large answer set.
Question by:jkfrench
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 13

Assisted Solution

riazpk earned 500 total points
ID: 35090759
Something like:

SQL> ed
Wrote file afiedt.buf

  1  SELECT empno, ename, job, max(length(ename)) over() mx_length
  2  FROM Scott.emp
  3* WHERE deptno = 10
SQL> /

     EMPNO ENAME                          JOB                          MX_LENGTH
---------- ------------------------------ --------------------------- ----------
      7782 CLARK                          MANAGER                              6
      7839 KING                           PRESIDENT                            6
      7934 MILLER                         CLERK                                6

SQL> ed
Wrote file afiedt.buf

  1  SELECT empno, ename, job, max(length(ename)) over() mx_length
  2  FROM Scott.emp
  3* WHERE deptno = 20
SQL> /

     EMPNO ENAME                          JOB                          MX_LENGTH
---------- ------------------------------ --------------------------- ----------
      7369 SMITH                          CLERK                                5
      7566 JONES                          MANAGER                              5
      7788 SCOTT                          ANALYST                              5
      7876 ADAMS                          CLERK                                5
      7902 FORD                           ANALYST                              5

SQL> ed
Wrote file afiedt.buf

  1  SELECT empno, ename, job, max(length(ename)) over() mx_length
  2  FROM Scott.emp
  3* WHERE deptno = 30
SQL> /

     EMPNO ENAME                          JOB                          MX_LENGTH
---------- ------------------------------ --------------------------- ----------
      7499 ALLEN                          SALESMAN                             6
      7521 WARD                           SALESMAN                             6
      7654 MARTIN                         SALESMAN                             6
      7698 BLAKE                          MANAGER                              6
      7844 TURNER                         SALESMAN                             6
      7900 JAMES                          CLERK                                6

Open in new window


Author Comment

ID: 35112637
Thanks for the reply, riazpk. Sorry for my late reply. I was out of the office in meetings.

That looks similar to what I described above with "select max(length(<col>)) from <table>". The problem is since I'm working with user-entered queries, I don't know in advance what tables and columns will be included in the select.

I'm looking for either how I can get that information back from my Perl DBI (and DBD::Oracle) result set, or a way to get the table.column fields to use in that query without having to completely parse the user input.

I just came across Perl's SQL::Abstract module. Maybe that will help with the parsing.

Accepted Solution

jkfrench earned 0 total points
ID: 35140241
I ended up solving this by using Perl::DBI to fetch the results into an array of array refs, then making a pass through the array to find the max length of each column. For the size of query results we normally get, this is still pretty fast.

Here is the Perl snippet:

my $dataref = $sth->fetchall_arrayref();
@colLenghts = maxColumnLength(@$dataref);

sub maxColumnLength {
  my @len = ();

  foreach my $rowref (@_) {
    for my $i (0..scalar(@$rowref)-1) {
      my $itemlen = length($rowref->[$i]);
      $len[$i] = $itemlen if ($itemlen > $len[$i]);
  return @len;

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

by:Suhas .
ID: 37300627
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Author Comment

ID: 37300628
I posted an answer to my own question. Should I go ahead and accept it as the solution?

Author Closing Comment

ID: 37335715
I ended up going about this a different way, but want to award points to riazpk for offering a solution.

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question