Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
P is for Practical
 
PerlMonks  

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

by erix (Priest)
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 SQL92 already prescribes INFORMATION_SCHEMA_CATALOG_NAME.

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


Comment on Re: DBI:how to get name of the db ?
Select or Download Code
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 ...

    select DISTINCT CATALOG_NAME from INFORMATION_SCHEMA.SCHEMATA does.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://943506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2014-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls