Create a stored procedure with if beetween queries MySQL - mysql

I am looking to convert this query from SQL Server to Mysql:
create procedure Valor
#valor int
as
begin
if #valor = 1
begin
select 1
end
else
begin
select 2
end
end

As its a function:
CREATE FUNCTION Valor (valor int)
returns int DETERMINISTIC
return if(valor=1,1,2);

This is what served me.
CREATE PROCEDURE mysp ()
BEGIN
IF ##server_id=2 THEN DROP DATABASE accounting; END IF;
END;

Related

MySQL procedure that return true / false

I have procedure in MySQL called 'luhn_'.
I would like to use it like function in my other procedure - in IF condition. So it should return me some output parameter (maybe true / false?)
What is the easiest way to implement something like that?
DELIMITER $$
DROP procedure if exists `luhn_`$$
CREATE procedure `luhn_` (IN input_string VARCHAR(256), IN input_lenght INTEGER)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght)
THEN SELECT count(*) from courrier_envoye; -- true
ELSE
SELECT * from courrier_envoye; -- false
END IF;
END$$
-- call luhn_('123456789',10);
if condition
...
if(luhn_('123456789',10) = true) THEN ...
ELSE ...
...
Creating a function would do it for you
DELIMITER $$
CREATE FUNCTION `luhn_`(input_string VARCHAR(256), input_lenght INTEGER) RETURNS tinyint(1)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght) THEN
return true;
ELSE
return false;
END IF;
END
Usage in a stored procedure
if(select luhn_(input_string,input_lenght)) then
select 'a';
else
select 'b';
end if;

multiple mysql stored procedure in a single call

I have a stored procedure like this and it's working fine:
$drop = $mysqli->query("DROP PROCEDURE IF EXISTS changegroup");
$initiate = $mysqli->query("
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;
");
$result = $mysqli->query("CALL changegroup($p1,$p2);");
My question is, can we put two SQL statements in a single procedure and execute the second procedure based on first like this:
BEGIN
SELECT * FROM ........
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;
In your stored procedure write
if <condition> then
your code
end if;
So your code should be like this
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
DECLARE totalcount Integer default 0;
SELECT count(*) into totalcount FROM yourtable where <condition>;
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
if(totalcount > 0) then
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
end if;
END;

How to return the table using mysql Function

I am using MySQL. I want to return the table using MySQL function. In SQL its working fine but not in MySQL. I attach my partial code
DELIMITER $$
CREATE FUNCTION myFunction() RETURNS #tmptable TABLE (item varchar(20))
BEGIN
insert into #tmptable(item) values('raja')
return
END; $$
Using functions you can not return a table.
However you can use stored procedure to return the table.
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_Name`(OUT po_ErrMessage VARCHAR(200))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET po_ErrMessage = 'Error in procedure sp_Name';
END;
SELECT * FROM table_name;
END

How to return a resultset from StoredProcedure using MySql?

I am writing a stored procedure using MySql which returns multiple rows using select statement.
My code is as below
drop procedure if exists GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SET #resultset = (SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER
Any examples of how to return a resultSet in storedProcedure?
Thanks
Gendaful
DROP PROCEDURE IF EXISTS GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER ;

Mysql stored function freezing

I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'