Solved

Finding max column length returned from Perl DBI Oracle query

Posted on 2011-03-09
7
475 Views
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:
$sth->{PRECISION}[$i]

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.
0
Comment
Question by:jkfrench
  • 4
7 Comments
 
LVL 13

Assisted Solution

by:riazpk
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

0
 

Author Comment

by:jkfrench
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.
0
 

Accepted Solution

by:
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


0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

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.
0
 

Author Comment

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

Author Closing Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now