Bug #13584 builtin funcDATABASE() inside SP returns name of the SP schema, not the caller
Submitted: 28 Sep 2005 20:45 Modified: 3 Nov 2005 0:54
Reporter: Roland Bouman Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Documentation Severity:S3 (Non-critical)
Version:5.0.13 OS:Windows (Win XP Professional SP2)
Assigned to: Paul DuBois CPU Architecture:Any

[28 Sep 2005 20:45] Roland Bouman
Description:
When used inside a stored procedure, the builtin function DATABASE() (and it's synonym, SCHEMA()) always returns the name of the schema in which the stored procedure resides, even if the stored procedure's schema is not the current database. 

It was expected that it would return the database/schema name of the current database, e.g. the schema of the caller, not the schema of the callee.

It is not clear from the documentation wheter this behaviour is intentional. If it is intentional, this behaviour should be documented. 

In this case, it would be nice to have an addition function, say CURRENT_DATABASE()/CURRENT_SCHEMA(), that does return the name of the caller's schema. 

How to repeat:
delimiter go

use test

create procedure get_schema_name()
select schema()
go

use mysql

call test.get_schema_name()
go

+----------+
| schema() |
+----------+
| test     |
+----------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Suggested fix:
Let SCHEMA() and DATABASE() return the name of the caller's current database, not the database in which the callee is defined.

Alternatively, create a set of functions that does expose this behaviour.
[3 Nov 2005 0:54] Paul DuBois
Thank you for your bug report. This issue has been addressed in the
documentation. The updated documentation will appear on our website
shortly, and will be included in the next release of the relevant
product(s).

Additional info:

I've documented that the result in a stored routine is the
owning database.
[3 Nov 2005 9:10] Roland Bouman
Thanks for clarifying that. 
Should I file a feature request for a function that retuns the current database?
[3 Nov 2005 16:12] Paul DuBois
Yes.