Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

"DESCRIBE" Command via DBI?

by Anonymous Monk
on Jan 12, 2006 at 21:53 UTC ( [id://522816]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to get a list of fields given a specific table name. The SQLPLUS command "DESCRIBE" is not available via the DBI. Is there an alternative? Many thanks!

Replies are listed 'Best First'.
Re: "DESCRIBE" Command via DBI?
by jZed (Prior) on Jan 12, 2006 at 22:11 UTC
    The simplest way to get the field names of a specific table is use the statement handle's NAME attribute:
    my $sth = $dbh->prepare("SELECT * FROM $table WHERE 1=0"); $sth->execute; my @fields = @{$sth->{NAME}}; $sth->finish;
    Note, most DBDs only populate NAME after the execute, and you need the finish() if you don't actually fetch rows. DBI also provides the table_info() and column_info() methods. Also, if there are commands like EXPLAIN in your variant of SQL, DBI will happily pass those along to your RDBMS.

    updateAlso note that if portablity is a concern, use $sth->{NAME_lc} or $sth->{NAME_uc} instead of $sth->{NAME}. That way you'll get the field names in the same case from all RDBMSs.

Re: "DESCRIBE" Command via DBI?
by ptum (Priest) on Jan 12, 2006 at 23:09 UTC

    By your reference to SQLPLUS I am cleverly able to deduce you are working with Oracle. Not to take away from [id://jZed]'s answer, you may require more meta-data about the table. :)

    There is a view in Oracle called 'ALL_TAB_COLUMNS' from which you can select COLUMN_NAME, DATA_TYPE, and all kinds of other cool information:

    select column_name, data_type from ALL_TAB_COLUMNS where table_name = +'FOO';

    Update: Here is the describe on ALL_TAB_COLUMNS for my version of Oracle (10.0.2):

    SQL> desc all_tab_columns; Name Null? Type ----------------------------------------- -------- ------------------ +---------- OWNER NOT NULL VARCHAR2(30) TABLE_NAME NOT NULL VARCHAR2(30) COLUMN_NAME NOT NULL VARCHAR2(30) DATA_TYPE VARCHAR2(106) DATA_TYPE_MOD VARCHAR2(3) DATA_TYPE_OWNER VARCHAR2(30) DATA_LENGTH NOT NULL NUMBER DATA_PRECISION NUMBER DATA_SCALE NUMBER NULLABLE VARCHAR2(1) COLUMN_ID NUMBER DEFAULT_LENGTH NUMBER DATA_DEFAULT LONG NUM_DISTINCT NUMBER LOW_VALUE RAW(32) HIGH_VALUE RAW(32) DENSITY NUMBER NUM_NULLS NUMBER NUM_BUCKETS NUMBER LAST_ANALYZED DATE SAMPLE_SIZE NUMBER CHARACTER_SET_NAME VARCHAR2(44) CHAR_COL_DECL_LENGTH NUMBER GLOBAL_STATS VARCHAR2(3) USER_STATS VARCHAR2(3) AVG_COL_LEN NUMBER CHAR_LENGTH NUMBER CHAR_USED VARCHAR2(1) V80_FMT_IMAGE VARCHAR2(3) DATA_UPGRADED VARCHAR2(3) HISTOGRAM VARCHAR2(15)

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Thank-You this was very helpful. I had the same question. Hat's off to you sir
Re: "DESCRIBE" Command via DBI?
by castaway (Parson) on Jan 13, 2006 at 09:57 UTC
    You can also use DBIs table_info() method.. assuming DBD::Oracle supports it..

    C.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found