Why my Procedure with CONCAT show me a NULL value? - mysql

I'm trying to run this code on MySQL WorkBench:
DELIMITER $$
CREATE PROCEDURE lavoratore_superficie(IN _superficie CHAR(5),
OUT codiciFiscali VARCHAR(255))
BEGIN
DECLARE finito INTEGER DEFAULT 0;
DECLARE codiceFiscale VARCHAR(255) DEFAULT '';
DECLARE cursoreCodici CURSOR FOR
SELECT distinct(TL.Capocantiere)
FROM superficie SU INNER JOIN lottomateriale LM on SU.Codice = LM.Superficie INNER JOIN lavoro LA ON LM.Lavoro = LA.Codice INNER JOIN divisionelavoro DT ON DT.Lavoro = LA.Codice
INNER JOIN turno TU ON TU.Codice = DT.Turno INNER JOIN turnolavoro TL ON TL.Turno = DT.Turno
WHERE SU.Codice = _superficie;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finito = 1;
OPEN cursoreCodici;
preleva: LOOP
FETCH cursoreCodici INTO codiceFiscale;
IF finito = 1 THEN
LEAVE preleva;
END IF;
SET codiciFiscali = CONCAT(codiceFiscale, ';', codiciFiscali);
END LOOP preleva;
CLOSE cursoreCodici;
END $$
DELIMITER ;
Then I try to visualize the result set of my query into a local variable with the CONCAT function:
SET #codiciFiscali = '';
CALL lavoratore_superficie('SU001', #codiciFiscali);
SELECT #codiciFiscali;
But after the CALL if I select #codiciFiscali the result set is NULL.

Since codiciFiscali is declared as an OUT parameter, the initial value of #codiciFiscali is not being passed to the procedure. So the variable defaults to NULL.
Either add
SET codiciFiscali = '';
before the loop, or change the parameter to INOUT so it will concatenate to the caller's value.

Related

how does mysql user defined function know a selected row was found?

a MYSQL user defined function selects a row from a table. How does the UDF code determine if the selected row was found in the table?
CREATE FUNCTION snippetFolder_folderPath(folder_id int)
RETURNS varchar(512)
BEGIN
declare vFolder_id int;
declare vParent_id int;
declare vPath varchar(512) default '';
declare vFolderName varchar(256) default '';
set vFolder_id = folder_id;
build_path:
while (vFolder_id > 0) do
/* -------- how to know this select statement returns a row?? ---------- */
select a.parent_id, a.folderName
into vParent_id, vFolderName
from SnippetFolder a
where a.folder_id = vFolder_id;
if vPath = ' ' then
set vPath = vFolderName;
else
set vPath = concat_ws( '/', vFolderName, vPath );
end if ;
set vFolder_id = vParent_id;
end while ;
return vPath;
END
https://dev.mysql.com/doc/refman/8.0/en/select-into.html says:
If the query returns no rows, a warning with error code 1329 occurs (No data), and the variable values remain unchanged.
So you could declare a continue handler on warnings, something like the example from the documentation:
BEGIN
DECLARE i INT DEFAULT 3;
DECLARE done INT DEFAULT FALSE;
retry:
REPEAT
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING
BEGIN
SET done = TRUE;
END;
IF done OR i < 0 THEN
LEAVE retry;
END IF;
SET i = i - 1;
END;
UNTIL FALSE END REPEAT;
END
I'll leave it to you to read the documentation and adapt that example to your table and your loop.
Alternatively, if you're using MySQL 8.0 you can use recursive common table expression:
CREATE FUNCTION snippetFolder_folderPath(vFolder_id int)
RETURNS varchar(512)
BEGIN
DECLARE vPath varchar(512) DEFAULT '';
WITH RECURSIVE cte AS (
SELECT folderName, parent_id, 0 AS height
FROM SnippetFolder WHERE folder_id = vFolder_id
UNION
SELECT f.folderName, f.parent_id, cte.height+1
FROM SnippetFolder AS f JOIN cte ON cte.parent_id = f.folder_id
)
SELECT GROUP_CONCAT(folderName ORDER BY height DESC SEPARATOR '/')
INTO vPath
FROM cte;
RETURN vPath;
END
The recursive CTE result is all the ancestors of the row matching vFolder_id, and then one can use GROUP_CONCAT() to concatenate them together as one string.

return null value in the JSON_EXTRACT

MyJsonArray
[{"ID":"D29","PersonID":"23616639"},{"ID":"D30","PersonID":"22629626"}]
and I want from sql Function set this array in to my Table but return null value in the variable and not set record in My database
my function:
DELIMITER $$
CREATE DEFINER=`toshiari`#`localhost` FUNCTION `setTitleRecords`(`Title` VARCHAR(166) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, `List` JSON) RETURNS int(4)
BEGIN
DECLARE Item INT;
DECLARE HolderLENGTH INT;
DECLARE ValidJson INT;
DECLARE ID VARCHAR(166);
DECLARE PersonID VARCHAR(166);
DECLARE S1 VARCHAR(166);
DECLARE S2 VARCHAR(166);
SET ValidJson = (SELECT JSON_VALID(List));
IF ValidJson = 1 THEN
SET HolderLENGTH = (SELECT JSON_LENGTH(List));
SET Item = 0;
WHILE Item < HolderLENGTH DO
SET S1 = CONCAT("'$[",Item, "].ID'");
SET S2 = CONCAT("'$[",Item, "].PersonID'");
SET ID = (SELECT JSON_EXTRACT(List,S1));
SET PersonID = (SELECT JSON_EXTRACT(List,S2));
INSERT INTO `Titles`(`ID`,`PersonID`,`Title`) VALUES (ID, PersonID, Title);
SET Item = Item + 1;
END WHILE;
RETURN 3;
ELSE
RETURN 2;
END IF;
END$$
DELIMITER ;
when I use this command in the Sql commands no problem and return true value
SELECT JSON_EXTRACT('[{"ID":"D29","PersonID":"23616639"},{"ID":"D30","PersonID":"22629626"}]','$[0].ID') return "D29"
return
"D29"
but in when run function from this code
return error and said:
SET #p0='DR'; SET #p1='[{\"ID\":\"D29\",\"PersonID\":\"23616639\"},{\"ID\":\"D30\",\"PersonID\":\"22629626\"}]'; SELECT `setTitleRecords`(#p0, #p1) AS `setTitleRecords`;
#4042 - Syntax error in JSON path in argument 2 to function 'json_extract' at position 1
I created a little test, in order to reproduce your issues. Basically you just need to redeclare S1 and S2, in the following way:
SET S1 = CONCAT('$[',Item,'].ID');
SET S2 = CONCAT('$[',Item,'].PersonID');
And that's it. You can check the test in the following url: https://www.db-fiddle.com/f/2TPgF868snjwcHN3uwoSEA/0

SQL stored procedure local variable error #1064

I am working for a little film database with php and mysql. And i got a problem when I want to create a stored procedure, that the local variable don't work in the case i want to use it.
delimiter //
### showFilm###
create procedure showFilm( in id INTEGER )
BEGIN
DECLARE tmp_Value varchar(50);
IF ( id = 0) THEN SET tmp_Value = "";
ELSE SET tmp_Value = concat('where = ', id);
END IF;
select f.Titel, f.Jahr, f.Bewertung, f.altersfreigabe, f.Beschreibung, f.Trailer, f.Dateipfad, f.Titelbild, f.Speichermedium, r.Vorname, r.Nachname, r.Land, r.Bild
from film as f
inner join regisseur as r
on r.idRegisseur = f.idFilm tmp_Value;
END;
//

MySQL IN Clause in SQL query with User Defined Function

I am using following query to update all the children of particular topic.
UPDATE topics SET reuse = 0 WHERE topic_id IN (SELECT GetChildTopics(187));
Where
SELECT GetChildTopics(187);
returns "188,190,189" but my update query is updating only first row with topic_id = 188, instead of updating first topic only, it should update all 3 topics.
When I put the values manually it works fine.
UPDATE topics SET reuse = 0 WHERE topic_id IN (188,190,189);
Can anyone suggest what's wrong I am doing here?
Here is the code for GetChildTopics MySQL Function
CREATE DEFINER=`root`#`localhost` FUNCTION `GetAncestry`(GivenID INT) RETURNS varchar(1024) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE rv VARCHAR(1024);
DECLARE cm CHAR(1);
DECLARE ch INT;
SET rv = '';
SET cm = '';
SET ch = GivenID;
WHILE ch > 0 DO
SELECT IFNULL(parent_topic_id,-1) INTO ch FROM
(SELECT parent_topic_id FROM topic_list WHERE id = ch) A;
IF ch > 0 THEN
SET rv = CONCAT(rv,cm,ch);
SET cm = ',';
END IF;
END WHILE;
RETURN rv;
END
Try this;)
UPDATE topics SET reuse = 0 WHERE FIND_IN_SET(topic_id, GetChildTopics(187));

mysql cursor - select IDs in stament

It returns IDs as string, it returns only one row which it has first ID. "7,6,5" It returns only 7; How can we correct this problem?
CREATE DEFINER=`root`#`localhost`
PROCEDURE `GET_COMPANION`(IN paramEmployeeId INT)
BEGIN
DECLARE counter INTEGER DEFAULT 0;
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE v_companion varchar(100) DEFAULT "";
DECLARE v_companion_list varchar(100) DEFAULT "";
-- declare cursor for companion_id
DEClARE companion_cursor CURSOR FOR
SELECT companion_id FROM employee_has_companion ehc
WHERE ehc.employee_id = paramEmployeeId;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN companion_cursor;
get_companion: LOOP
FETCH companion_cursor INTO v_companion;
IF v_finished = 1 THEN
LEAVE get_companion;
END IF;
-- build companion list
SET v_companion_list = CONCAT(v_companion,",",v_companion_list);
END LOOP get_companion;
CLOSE companion_cursor;
SELECT * FROM companion co
LEFT JOIN citizenship ci
ON co.citizenship_id = ci.citizenship_id
WHERE co.companion_id IN( SUBSTRING(v_companion_list,
1,LENGTH(v_companion_list)-1));
END
You can use FIND_IN_SET(str,strlist) to check existence of a string in a list of comma separated values.
Change:
SELECT * FROM companion co
LEFT JOIN citizenship ci
ON co.citizenship_id = ci.citizenship_id
WHERE co.companion_id IN( SUBSTRING(v_companion_list,
1,LENGTH(v_companion_list)-1));
To:
SELECT * FROM companion co
LEFT JOIN citizenship ci
ON co.citizenship_id = ci.citizenship_id
WHERE FIND_IN_SET( co.companion_id, v_companion_list ) > 0;