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