STORED PROCEDURE does not exist - mysql

I create stored procedure from mysql client terminal and everything is OK.
But when I try to call it i get this error message:
ERROR 1305 (42000): PROCEDURE XXX does not exist
After that i try to create it again without
DROP PROCEDURE IF EXISTS
statement and I get this:
ERROR 1304 (42000): PROCEDURE XXX already exists
What's wrong?
*THE PROBLEM WAS THAT MY DATABASE HAVE POINT (.) IN NAME *
*EXAMPLE: 'site.db' -> THIS IS WRONG NAME OF DATABASE AND MYSQL CAN'T FIND PROCEDURE !!!*

Possibly you have problems with consistency of your system databases after incorrect upgrade or something like that.
What are results for
select * from information_schema.ROUTINES where routine_name = 'xxx'

When you are define procedure using mysql client,
you could using root user (or user A).
Chances are you are using another user to call the store procedure,
let's said user B, it could causing some differences on the privileges
If this is the case, you can grant the access right
To view the current privilege, you can make use of this command
show procedure status;

Related

MySQL grants to create a stored procedure on performance_schema.*?

I understand why this is so locked down. If I were MySQL, I wouldn't want folks to (easily) shoot themselves in the foot on business critical/internal-use databases either. Nevertheless, it should support legitimate use cases.
Suppose I had a stored procedure:
delimiter //
CREATE DEFINER=CURRENT_USER() PROCEDURE performance_schema.sp_flush_hosts()
BEGIN
-- requires DROP privileges
TRUNCATE TABLE performance_schema.host_cache;
END
//
delimiter ;
for a user called root with ALL permissions on most databases.
In practice, MySQL seems to explicitly forbid this for performance_schema:
Because only a limited set of privileges apply to Performance Schema tables, attempts to use GRANT ALL as shorthand for granting privileges at the database or table leval fail with an error:
Fine. So let's grant some specific permissions:
mysql> GRANT CREATE ROUTINE on perforamnce_schema.host_cache to 'root'#'%';
ERROR 1144 (42000): Illegal GRANT/REVOKE command; please consult the manual to see which privileges can be used
In the same document (above), only: SELECT, UPDATE, DROP are singled out, but it does not say if those are the only available permissions. How would I grant permissions to create this routine? Is that even possible? Thanks.
Inspiration taken from this SO answer: Is there a way to effectively GRANT on either TRUNCATE or DROP TABLE in MySQL?
Perhaps:
Put your SP in your database. Or perhaps a generic database of "utilities".
Be "root" when you CREATE the SP, and declare it with SQL SECURITY DEFINER See https://dev.mysql.com/doc/refman/5.6/en/stored-objects-security.html#stored-objects-security-sql-security This provides a way to elevate the permissions just for the running of the SP. Normally, one would use SQL SECURITY INVOKER -- to use the permissions of the person CALLing the SP.

ERROR 1146 (42S02): Table 'mysql.proc' doesn't exist while calling a stored procedure

I am using this thread -
Rename a mysql procedure
to rename a stored procedure
Here upon trying the command as shown in the answer -
UPDATE `mysql`.`proc`
SET name = '<new_proc_name>',
specific_name = '<new_proc_name>'
WHERE db = '<database>' AND
name = '<old_proc_name>';
I get the error -
ERROR 1146 (42S02): Table 'mysql.proc' doesn't exist while calling a stored procedure
Here regarding the other questions regarding mysql.proc does not exit, none address the specific problem of calling a stored procedure.
The mysql.proc table was removed in MySQL 8.0. See No more mysql.proc in MySQL 8.0
You can use information_schema.routines to get information about stored procedures. But this is a read-only view, you can't update it. So I don't think there's any simple way to rename procedures any more. You may be able to use dynamic SQL to define the procedure with the new name using this information.
EDIT:
Unfortunately, the above is not possible just in MySQL, because CREATE PROCEDURE can't be executed using PREPARE, and information_schema.routines doesn't contain all the information needed to recreate the procedure. You could do it in an external language by performing a SHOW CREATE PROCEDURE query and then replacing the name to form a new query.
Recommend avoid fiddling with any mysql table directly.
Use show create procedure old_proc_name
And then create procedure new_proc_name ....
And drop the old drop procedure old_proc_name

Possible to have multiple users as DEFINER for MySQL Stored Procedure?

I'm struggling a little with MySQL stored procedures and getting a bit frustrated. I have a set of SPs created by Bob. As he is the DEFINER, only he can see the CREATE statement for them, amend them etc.
Mary can see Bob's stored procedures in the schema in MySQL Workbench, but can't see what they do - when she clicks on the SP and selects "Send to SQL Editor -> CREATE statement" nothing happens, because she's not the definer.
Coming from a MS SQL background this is a little bizarre to me. Is there any way I can set up a group (e.g. "DB_DEVS") and make this group the definer of the Stored Procs so Bob and Mary can see each other's code?
DEFINER always refers to the user who created the stored procedure. This can only be 1 user.
If Mary wants to see Bobs procedure, she could call:
SHOW CREATE PROCEDURE proc_name
To see the code of the procedure. She could also call the following to see the code:
SELECT ROUTINE_DEFINITION FROM information_schema.ROUTINES WHERE SPECIFIC_NAME='proc_name'
Heres how to enable Mary to access the view of the procedure via MySQL-Workbench:
By default, Mary is not able to send the create statement to the SQL-Editor. But this is just a privilege thing. Mary just needs a basic SELECT privilege in the mysql.proc table. To do this, run the following SQL-Statement (via Command line or directly in the Workbench):
GRANT SELECT ON mysql.proc TO 'mary'#'%'
This command enables Mary to access the Create-Statement from all hosts. If you want to limit it to a specific host you would do something like:
GRANT SELECT ON mysql.proc TO 'mary'#'192.168.2.1'
If Mary has the SELECT privilege she should be able to see the procedure after doing Send to SQL Editor -> CREATE statement
NOTE: In order to run the GRANT-Command you need to be logged in as user who has the privilege to grant privileges (e.g. root-user)
+++++++++++++++++++++++++EDIT+++++++++++++++++++++++++
There is a "quick and dirty" way to achieve this for a large number of users without writing for each user a new command to grant the privilege:
(make sure to do this with a user who has the privilege to insert rows via the Workbench)
Open a new SQL-Tab in your Workbench
Type SELECT * FROM mysql.tables_priv; and run it
Above the result-grid there should be the a small button which allows you to import data from a csv-File.
Create a CSV-File which looks like this:
%,mysql,jane,proc,root#localhost,"2016-02-19 22:51:47",Select,
%,mysql,max,proc,root#localhost,"2016-02-19 22:51:47",Select,
%,mysql,steve,proc,root#localhost,"2016-02-19 22:51:47",Select,
%,mysql,greg,proc,root#localhost,"2016-02-19 22:51:47",Select,
%,mysql,jamie,proc,root#localhost,"2016-02-19 22:51:47",Select,
...further users
jane, max, steve,... would be your users. Leave the other columns the way they are.
Import your csv-File
Run FLUSH PRIVILEGES in an SQL-Window (reloades privileges from priv-tables)
Finished! All your users can now access Stored Procedures

SHOW CREATE PROCEDURE inconsistent results with perl DBI

Piping "SHOW CREATE PROCEDURE foo" into mysql results include the complete procedure definition in a column labeled "Create Procedure". However,
$dbh->selectrow_hashref("SHOW CREATE PROCEDURE foo");
results in $ref->{'Create Procedure'} being undef, with other columns correctly populated.
Both executed on the same machine with the same credentials.
Does anyone know why?
This is what you would see if the user you connect as doesn't have permission to see the procedure. Try using the same user on the command line and I'd guess you will see NULL for the Create Procedure column.
You appear to need select privilege on mysql.proc to see procedure bodies.

mysql display list of user defined functions in phpmyadmin

How can I view the list of user defined functions in mysql database using phpmyadmin.
Mysql database has been migrated from one server to another server and user defined custom functions are not working. I need to view the list of user defined function to check whether they exist in database or not.
Fatal error: db::execute() Could not execute: FUNCTION database.xxx does not exist (SQL: SELECT Function(field) FROM users in file.php on line xx
The following MySQL query will list the user-defined routines.
select * from information_schema.routines;
That will give you all the information about your custom functions/procedures:
select specific_name, definer from information_schema.routines where definer not like '%mysql%';
hope it helps!