thames
thames

Reputation: 6083

In MySQL is there a variable for the current database?

I have a script that I run on multiple databases and to run on multiple databases I just change the MySql "USE" statement.

I'm wanting to have a select statement that outputs the current database the script is executing against as a field.

For example:


USE my_db;

SELECT 
  CURRENT_DB, -- this is where to insert the current executing db, i.e. "my_db"
  id, name, blah, blah
FROM my_table

Upvotes: 31

Views: 28336

Answers (3)

srini
srini

Reputation: 884

use DATABASE()

Returns the default (current) database name as a string in the utf8 character set. If there is no default database, DATABASE() returns NULL. Within a stored routine, the default database is the database that the routine is associated with, which is not necessarily the same as the database that is the default in the calling context.

for Example

  mysql> SELECT DATABASE();

Upvotes: 24

John P
John P

Reputation: 15245

Try this snippet:

select database();

Upvotes: 15

dcp
dcp

Reputation: 55464

Use the database() function: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_database

Upvotes: 48

Related Questions