I have created the following stored procedure in phpMyAdmin Version: 4.1.14.3 running on a ZYXEL NS325 NAS. It appears in the routines list for the database.
DROP PROCEDURE `getDuplicateApplicationCyclePersons`;
CREATE DEFINER=`root`#`localhost`
PROCEDURE `getDuplicateApplicationCyclePersons`()
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
SELECT * FROM ApplicationCycle
However, when I execute the routine from the routines list, I get "the following query failed" but with no error details i.e. "MySql said:"
The query runs in the SQL window. I have tried several simple procedures on this new database all with the same result. Any ideas why they wont run?
Related
I want to run sql script which include db and table creations and stored procedure creations.
but when I try to run sql script using execute sql script keyword in database library I get an error like below
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $$\n CREATE OR
REPLACE PROCEDURE `proc_GetCustomerDetails`(\n I...' at line 2")
before procedure I have delimiter like this,
DELIMITER $$
CREATE OR REPLACE PROCEDURE `proc_GetCustomerDetails`(
IN CustomerNbr LONGTEXT,
IN Lang VARCHAR(5)
)
DETERMINISTIC
BEGIN
IF Lang IS NULL THEN SET lang = "fin";
END IF;
SELECT * from dbname.customer;
END;$$
DELIMITER ;
If I comment stored procedure part, sql file is running without errors with rest of the table creation statements.
I googled this and couldn't find any related issue. I saw we have call stored procedure keyword. but I want to keep table creations and stored procedures in same sql file and need to run. I use MariaDB for this task.
Libraries used,
pymysql
robotframework-databaselibrary
If I run sql file using HeidiSQL it is running without any errors with procedures and delimiters. That mean there are no sql errors.
Can Someone tell me how to fix this?
DELIMITER is a statement supported only for the client, it is not supported by the server; thus the error. The solution - drop it.
Here's a question with very good answers what is it and why it's needed.
In short - when you work with a client you need a way to instruct it "this is not a statement you should execute immediately, this is still just a line in the SP you'll be sending to the server" - so you tell (to the client) "the DELIMITER b/n statements is temporarily $$". The server doesn't need/care about that - it knows everything between CREATE PROCEDURE, BEGIN, END are connected statements, a block.
When you connect to the DB through API (pymysql) vs an interactive client (shell, heidisql, etc) - you're sending the SP as a block, there's no way its statements will be ran one by one, thus the DELIMITER is not needed, not a supported command by the server, and generates an error. Drop it.
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
I have two MySQL users:
mysql_user_1
mysql_user_2 (EXECUTE permission only)
mysql_user_1 created a stored procedure getRecord(int uid_of_record) which run a SELECT * FROM TABLE WHERE uid=?. Can mysql_user_2 execute that stored procedure without having SELECT privilege?
This is explained in detail in the MySQL documentation Access Control for Stored Programs and Views.
A stored procedure can be defined with either:
SQL SECURITY DEFINER
or
SQL SECURITY INVOKER
If it's DEFINER, the procedure executes with the permissions of the user who defined the procedure; in your case, if mysql_user_1 has SELECT privilege, the procedure will work for mysql_user_2.
If it's INVOKER, the procedure executes with the permissions of the user running the procedure. The procedure won't work for mysql_user_2 in this case.
If you don't have this clause in the procedure, it defaults to DEFINER.
I know that the function of print can print the sql sentence in sql server,I want to print the sql sentence of the procedure in mysql,the function of print is invalid.how can I print in mysql?
You can use SHOW PROCEDURE CODE command in mysql to view the procedure sql.
mysql> SHOW PROCEDURE CODE procedure_name
This will not show the actual query to create the stored procedure, but it will show the query (SELECT/UPDATE/INSERT/DELETE/etc) present inside stored procedure.
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.