Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: DBI:how to get name of the db ?

by erix (Vicar)
on Dec 14, 2011 at 09:22 UTC ( #943506=note: print w/replies, xml ) Need Help??

in reply to DBI:how to get name of the db ?

For the sake of completeness: SQL-standard compliant databases have table information_schema_catalog_name, which always contains one row and one column containing the name of the current database (the current 'catalog', in SQL terminology).

(The SQL standard says "The value of CATALOG_NAME is the name of the catalog in which this Information Schema resides.")

PostgreSQL example:

testdb=# select CATALOG_NAME from INFORMATION_SCHEMA.INFORMATION_SCHEM +A_CATALOG_NAME; catalog_name -------------- testdb (1 row)

I'd normally prefer DBI, but the SQL can be handy.

(And FWIW: postgres also supports the more succinct and standard-compliant CURRENT_CATALOG:)

testdb=# select current_catalog; current_database ------------------ testdb (1 row)

And then there is DBI's get_info stuff:

use DBI::Const::GetInfoType; say "SQL_DBMS_NAME [",$dbh->get_info($GetInfoType{SQL_DBMS_NAME + }),"]"; say "SQL_DBMS_VER [",$dbh->get_info($GetInfoType{SQL_DBMS_VER + }),"]"; say "SQL_DATABASE_NAME [",$dbh->get_info($GetInfoType{SQL_DATABASE_NAM +E}),"]"; say "SQL_SERVER_NAME [",$dbh->get_info($GetInfoType{SQL_SERVER_NAME + }),"]"; -- output SQL_DBMS_NAME [PostgreSQL] SQL_DBMS_VER [09.02.0000] SQL_DATABASE_NAME [testdb] SQL_SERVER_NAME [testdb]

update: Jenda: Re SQL-standard versions: PostgreSQL supports generally quite recent versions: SQL:2011 and SQL:2008. And even SQL92 already prescribed INFORMATION_SCHEMA_CATALOG_NAME.

Some systems are more SQL-compliant than others. PostgreSQL tries quite hard to implement as much as possible.

Replies are listed 'Best First'.
Re^2: DBI:how to get name of the db ?
by Jenda (Abbot) on Dec 15, 2011 at 14:16 UTC

    Doesn't work in Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (X64) (Build 7601: Service Pack 1). I wonder what version of the standard ...


    Enoch was right!
    Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943506]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2017-02-21 10:17 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (309 votes). Check out past polls.