Get current executing stored procedure name in MySQL? - mysql

I need a log for the stored procedures i have written in MySQL.
I know this is available in MS SQL Server as ##procid.
What is the equivalent in MySQL?
I'm going to use a timestamp, connection_id, database().
How can i get the name of the sp i am executing?
How about the sp that called me?
thanks,
adam

You can pass a procedure name as an IN parameter into called procedures, and log this information from these procedures.
For example -
DELIMITER $$
CREATE PROCEDURE procedure1(IN proc_name VARCHAR(255))
BEGIN
INSERT INTO proc_log VALUES('procedure1', proc_name, NOW());
END$$
CREATE PROCEDURE procedure2(IN proc_name VARCHAR(255))
BEGIN
INSERT INTO proc_log VALUES('procedure2', proc_name, NOW());
CALL procedure1('procedure2');
END$$
DELIMITER ;
CALL procedure2(NULL);
SELECT * FROM proc_log;
+------------+----------------+---------------------+
| proc_name | call_proc_name | call_ts |
+------------+----------------+---------------------+
| procedure2 | NULL | 2012-07-30 16:17:53 |
| procedure1 | procedure2 | 2012-07-30 16:17:53 |
+------------+----------------+---------------------+

Related

How to set varchar datatype in function?

I have an issue in my code where i took this : (ApliEMAIL int) in below code but when i execute function it return empty values because in table i took email filed as varchar.
When i write code with this (ApliEMAIL varchar) it does not create function and gives error.
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL int)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
I tested your function. I got this error.
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
By the way, in the future, when you are asking for help with an error, include the error message in your post. Help us to help you! Don't make us guess!
Read the documentation about READS SQL DATA here: https://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html
So I added that option to your function:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
It works now:
mysql> select get_appli_name_by_email('thefrog#muppets.org');
+------------------------------------------------+
| get_appli_name_by_email('thefrog#muppets.org') |
+------------------------------------------------+
| Kermit |
+------------------------------------------------+
You are missing the length of the ApliEMAIL-parameter.
Insted of:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
you should have:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(255))
..or whatever the length of ApliEMAIL is.
You must add deterministic or reads sql data flag.
DELIMITER $$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(50))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
RETURN ifnull((SELECT apli_fname FROM tbl_signup WHERE apli_email = ApliEMAIL LIMIT 1), '');
END$$
DELIMITER ;

stored procedure with parameters in MYSQL

I have a question about the stored procedure:
Create a stored procedure named format_currency that accepts a character and a double number. It will return a VARCHAR(32) with the symbol in the front, followed by the number to 2 decimal places.
For example, format_currency('$', 123.4) should return $123.40
Here is my code:
CREATE OR REPLACE PROCEDURE format_currency(IN c VARCHAR2, IN n DOUBLE(9,2))
AS
BEGIN
SELECT CONCAT(c,',' n);
END;
It's not working, I have no idea how to write codes inside BEGIN and END.
Many Thanks for your help.
Henry
Instead of procedure I suggest to use function:
Code:
DELIMITER $$
DROP FUNCTION IF EXISTS test_f$$
CREATE FUNCTION test_f(a varchar(22),b double(9,2)) returns varchar(64)
BEGIN
SET #RET = (select concat(a,b));
return #RET;
END$$
DELIMITER ;
Test Run:
mysql> select test_f('$',123);
+-----------------+
| test_f('$',123) |
+-----------------+
| $123.00 |
+-----------------+
1 row in set (0.10 sec)
mysql>
DELIMITER\\
CREATE PROCEDURE format_currency (a CHAR, b DOUBLE(9,2))
BEGIN
declare ret VARCHAR(32);
set #ret = (SELECT CONCAT(a,b));
select #ret;
END
DELIMITER\\
call format_currency('$',123);
I had a similar question and nothing the other people answered worked for me in MySQL. Here is a working solution with 2 parameters as "SIGN" and "MONEY" and creating a variable called "NEWRESULT" that you delcare, set as 0, and then set as the concat of "SIGN" and "MONEY".
DELIMITER //
CREATE PROCEDURE format_currency`enter code here`
(IN SIGN char(1),IN MONEY double(9,2))
BEGIN
declare NEWRESULT char(8);
set NEWRESULT=0;
SET NEWRESULT=(SELECT CONCAT(SIGN,MONEY));
SELECT NEWRESULT;
END//
DELIMITER ;

the output param can be bind by name, not by sort?

In the below code,"out b char(8)" can only bind to the sort of "#bz:=cName,#cz:=cSex",
can i bind it by name(b)?
delimiter $$
drop procedure if exists test_out1 $$
create procedure test_out1(in a CHAR(1), out b char(8),out c char(1) )
begin
select #bz:=cName,#cz:=cSex from students where cID=a;
end $$
delimiter ;
call test_out1('2',#bx,#cx);
SELECT #bx,#cx
-----------------------------------------V2
when I use
create procedure test_out2(in a CHAR(1), out b char(8),out c char(50) )
begin
select cName into b from students where cID=a;
end $$
delimiter ;
-->it OK
but whe i use :
create procedure test_out2(in a CHAR(1), out b char(8),out c char(50) )
begin
select cName into b,cSex to c from students where cID=a;
end $$
-->ERROR,
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
You already have OUT parameters (b and c) defined in the procedure definition.
You need not explicitly define the results to other variables, but out parameters.
Change:
begin
select #bz:=cName,#cz:=cSex from students where cID=a;
end $$
to:
begin
select cName, cSex into b, c from students where cID=a;
end $$
And when you execute:
call test_out1( '2', #bx, #cx );
SELECT #bx, #cx ;
resulting column values from procedure will be assigned to #bx and #cx session variables.
Example:
delimiter //
CREATE PROCEDURE simpleproc (OUT dt date, OUT ts datetime)
BEGIN
SELECT current_date, now() INTO dt, ts;
END//
delimiter ;
Now, call this for a test:
CALL simpleproc( #dt, #ts );
select #dt, #ts;
+------------+---------------------+
| #dt | #ts |
+------------+---------------------+
| 2014-03-13 | 2014-03-13 11:19:03 |
+------------+---------------------+
Refer To:
MySQL: CREATE PROCEDURE and CREATE FUNCTION Syntax

Stored procedure MSSQL to MYSQL

I need convert this stored procedure mssql to mysql, somebody help me please
1)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_IS]
as
insert into request (dateIS) values (GETDATE())
2)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_RN]
as
select COUNT(id) "quantity" from notify
THANKS!
Look at this
CREATE
[DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
routine_body:
Valid SQL routine statement
Modify a Stored Procedure
MySQL provides an ALTER PROCEDURE statement to modify a routine, but only allows for the ability to change certain characteristics. If you need to alter the body or the parameters, you must drop and recreate the procedure.
DROP PROCEDURE IF EXISTS sp_IS;
delimiter //
create PROCEDURE sp_IS
BEGIN
insert into request (dateIS) values (GETDATE())
END
delimiter;
DROP PROCEDURE IF EXISTS sp_RN;
delimiter //
create PROCEDURE sp_RN
BEGIN
select COUNT(id) AS quantity from notify
END
delimiter;

MySQL save results of EXECUTE in a variable?

How do I save the results of EXECUTE statement to a variable? Something like
SET a = (EXECUTE stmtl);
If you want to do this with a prepared statement, then you need to include the variable assignment in the original statement declaration.
If you want to use a stored routine it's easier. You can assign the return value of a stored function directly to a variable, and stored procedures support out parameters.
Examples:
Prepared Statement:
PREPARE square_stmt from 'select pow(?,2) into #outvar';
set #invar = 1;
execute square_stmt using #invar;
select #outvar;
+---------+
| #outvar |
+---------+
| 1 |
+---------+
DEALLOCATE PREPARE square_stmt;
Stored Function:
delimiter $$
create function square_func(p_input int) returns int
begin
return pow(p_input,2);
end $$
delimiter ;
set #outvar = square_func(2);
select #outvar;
+---------+
| #outvar |
+---------+
| 4 |
+---------+
Stored Procedure:
delimiter $$
create procedure square_proc(p_input int, p_output int)
begin
set p_output = pow(p_input,2);
end $$
delimiter ;
set #outvar = square_func(3);
call square_proc(2,#outvar);
select #outvar;
+---------+
| #outvar |
+---------+
| 9 |
+---------+