Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by erix (Prior)
on Dec 14, 2011 at 09:22 UTC ( [id://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 ...

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://943506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found