What seems to be the error here in the below code? - mysql

Error Code: 2014 Commands out of sync; you can't run this command now
Platform- My SQL Workbench
DELIMITER//
DROP PROCEDURE IF EXISTS must_watch_movies;
CREATE PROCEDURE
must_watch_movies ()
BEGIN DECLARE mTitle
VARCHAR (45);
DECLARE mDistributor VARCHAR (45);
DECLARE mRelease datetime;
DECLARE result VARCHAR (1000);
DECLARE no_records INTEGER DEFAULT FALSE;
DECLARE cursor_movies CURSOR FOR
SELECT title, Distributor, year (release_date)
FROM movies WHERE gross > 200000000 ORDER BY title;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET SET no_records = TRUE;
OPEN cursor_movies;
WHILE no_records = FALSE DO
FETCH cursor_movies INTO mTitle, mDistributor, mRelease;
SET result =
CONCAT ("'", mTitle, "','" mDistributor, "','" mRelease, "|");
END WHILE;
CLOSE cursor_movies;
SELECT result AS "Output";
END//
DELIMITER;
CALL must_watch_movies();
The declared values match the datatypes for actual column values in the table.
like mTitle and title are the same type

You have some errors in your code
A double SET when you decalre a handler
You have to concat result if you want all movies.
The out put must be some what more complicated, but i leave that to you
CREATE TABLE movies (title varchar(10),Distributor varchar(19), release_date date,gross BIGINT)
INSERT INTO movies VALUES('text1','text2', NOW(),200000001),('text3','text4', NOW(),200000001),('text5','text6', NOW(),200000001)
CREATE PROCEDURE
must_watch_movies ()
BEGIN DECLARE mTitle
VARCHAR (45);
DECLARE mDistributor VARCHAR (45);
DECLARE mRelease INT;
DECLARE result VARCHAR (1000) DEFAULT "";
DECLARE no_records INTEGER DEFAULT FALSE;
DECLARE cursor_movies CURSOR FOR
SELECT title, Distributor, year (release_date)
FROM movies WHERE gross > 200000000 ORDER BY title;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_records = TRUE;
OPEN cursor_movies;
WHILE no_records = FALSE DO
FETCH cursor_movies INTO mTitle, mDistributor, mRelease;
SET result = CONCAT (result,"'", mTitle, "','", mDistributor, "','", mRelease, "|");
END WHILE;
CLOSE cursor_movies;
SELECT result AS "Output";
END
CALL must_watch_movies()
| Output |
| :--------------------------------------------------------------------------------------- |
| 'text1','text2','2021|'text3','text4','2021|'text5','text6','2021|'text5','text6','2021| |
✓
db<>fiddle here

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.

MySql Fetch cursor into variable return null

The problem is that the FETCH INTO (in the loop) does not put the value into the variable. I've looked at MYSQL | SP | CURSOR - Fetch cursor into variable return null but the table is already populated.
The transaction table looks like this:
CREATE TABLE `transactionentry` (
`transactionid` bigint(20) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`transactionid`),
...
) ENGINE=InnoDB AUTO_INCREMENT=651 DEFAULT CHARSET=utf8;
The stored procedure:
PROCEDURE `doTxnHouseKeeping`()
BEGIN
-- Loop invariant
DECLARE noEntries INTEGER DEFAULT FALSE;
-- Error codes
DECLARE code CHAR(5) DEFAULT '00000';
DECLARE msg TEXT;
-- Txn vars
DECLARE transactionId BIGINT(20);
DECLARE lastTransactionId BIGINT(20) DEFAULT 0;
-- testing
DECLARE counter INT(11) DEFAULT 0;
DEClARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
DECLARE CONTINUE HANDLER FOR
NOT FOUND SET noEntries = TRUE;
DECLARE EXIT HANDLER FOR
SQLEXCEPTION
BEGIN
GET DIAGNOSTICS CONDITION 1
code = RETURNED_SQLSTATE, msg = MESSAGE_TEXT;
SELECT CONCAT('Error fetching transaction entries code: ', code, ' message: ', msg);
END;
OPEN txnEntryCur;
mainLoop: LOOP
FETCH
txnEntryCur
INTO
transactionId;
IF noEntries THEN
LEAVE mainLoop;
END IF;
IF transactionId IS NOT NULL THEN
INSERT INTO debugTable (`bigintval`) VALUES (transactionId);
ELSE
INSERT INTO debugTable (`strval`) VALUES ('transactionId is NULL');
END IF;
SET counter = counter + 1;
END LOOP mainLoop;
CLOSE txnEntryCur;
SELECT CONCAT("Count: ", counter);
END
Running the stored procedure returns this result:
+--------------------------+
|CONCAT("Count: ", counter)|
+--------------------------+
| Count: 1|
+--------------------------+
The result in the debug table is:
+------------+---------+-----------------------+
|iddebugTable|bigintval| strval|
+------------+---------+-----------------------+
| 1| NULL|"transactionId is NULL"|
+------------+---------+-----------------------+
Which means that the value was not copied in
When running the SQL (as it is in the stored procedure), it returns:
+-------------+
|transactionid|
+-------------+
| 591|
+-------------+
I found the problem and it is weird. It doesn't cause any error and / or exceptions, just doesn't put any values into the variables. The solution is to change the cursor declare statement from:
DECLARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
To:
DECLARE txnEntryCur CURSOR FOR
SELECT
`transactionentry`.`transactionid`
FROM
`transactionentry`
LIMIT 1;
Not even the documentation indicated that it might have been a problem (https://dev.mysql.com/doc/refman/5.7/en/declare-cursor.html)
I only fully qualify the SELECT (and WHERE) part of the SQL statement if I'm selecting from more than one table and thus never picked this up on more complex queries.
I hope this will save someone some time in the future.
Your problem is here:
DECLARE transactionId BIGINT(20);
You declare a variable named transactionId so when you do this:
DEClARE txnEntryCur CURSOR FOR
SELECT
`transactionid`
FROM
`transactionentry`
LIMIT 1;
Your cursor's select is picking up the variable you declared which is why fully qualifying the field works. However, if you don't want to fully qualify the field in your select you can rename your variable.
Try giving the variable transactionId a default value
...
DECLARE transactionId BIGINT(20) DEFAULT 0
...
and also replace
DECLARE noEntries INTEGER DEFAULT FALSE;
with
DECLARE noEntries BOOLEAN DEFAULT FALSE;
since you want to use it as a BOOLEAN value and set it to TRUE later in the procedure.

How to convert TSQL query into MYSQL query?

I have developed a function for split string in tsql but mysql don't have some built in functions. I needed to function in MYSQL as i am new in mysql. Function should accept 2 parameters
1. String to be split
2. separator (',' or whatever)
Kindly reply me.
i had found solution on the internet you can into that.
DELIMITER //
DROP FUNCTION IF EXISTS `splitAndTranslate` //
CREATE FUNCTION splitAndTranslate(str TEXT, delim VARCHAR(124))
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 0; -- total number of delimiters
DECLARE ctr INT DEFAULT 0; -- counter for the loop
DECLARE str_len INT; -- string length,self explanatory
DECLARE out_str text DEFAULT ''; -- return string holder
DECLARE temp_str text DEFAULT ''; -- temporary string holder
DECLARE temp_val VARCHAR(255) DEFAULT ''; -- temporary string holder for query
-- get length
SET str_len=LENGTH(str);
SET i = (LENGTH(str)-LENGTH(REPLACE(str, delim, '')))/LENGTH(delim) + 1;
-- get total number delimeters and add 1
-- add 1 since total separated values are 1 more than the number of delimiters
-- start of while loop
WHILE(ctr<i) DO
-- add 1 to the counter, which will also be used to get the value of the string
SET ctr=ctr+1;
-- get value separated by delimiter using ctr as the index
SET temp_str = REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, ctr), LENGTH(SUBSTRING_INDEX(str, delim,ctr - 1)) + 1), delim, '');
-- query real value and insert into temporary value holder, temp_str contains the exploded ID
SELECT <real_value_column> INTO temp_val FROM <my_table> WHERE <table_id>=temp_str;
-- concat real value into output string separated by delimiter
SET out_str=CONCAT(out_str, temp_val, ',');
END WHILE;
-- end of while loop
-- trim delimiter from end of string
SET out_str=TRIM(TRAILING delim FROM out_str);
RETURN(out_str); -- return
END//
reference http://www.slickdev.com/2008/09/15/mysql-query-real-values-from-delimiter-separated-string-ids/
In mysql they they dont support some functionality like sqlserver. so spliting will be difficult in mysql
SELECT e.`studentId`, SPLIT(",", c.`courseNames`)[e.`courseId`]
FROM ..
SELECT TRIM(SUBSTRING_INDEX(yourcolumn,',',1)), TRIM(SUBSTRING_INDEX(yourcolumn,',',-1)) FROM yourtable
CREATE FUNCTION [dbo].[SplitString]
(
#RowData nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table
(
--Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare #Cnt int
Set #Cnt = 1
While (Charindex(#SplitOn,#RowData)>0)
Begin
Insert Into #RtnValue (data)
Select
Data = ltrim(rtrim(Substring(#RowData,1,Charindex(#SplitOn,#RowData)-1)))
Set #RowData = Substring(#RowData,Charindex(#SplitOn,#RowData)+1,len(#RowData))
Set #Cnt = #Cnt + 1
End
Insert Into #RtnValue (data)
Select Data = ltrim(rtrim(#RowData))
Return
END

Procedures always returns empty results

I cant get this code to return me anything, i used to have the code in 2 functions and it always returned a random first name and a random last name but ever since i tried putting the code into this procedure it doesnt return anything, either empty results or just nothing happens after submitting the CALL command
DROP PROCEDURE IF EXISTS IdGenerator;
DELIMITER $$
CREATE PROCEDURE IdGenerator(tempCntry varchar(255))
BEGIN
DECLARE rndm1 INT;
DECLARE rndm2 INT;
DECLARE rndmPlier INT;
DECLARE firstN varchar(255);
DECLARE lastN varchar(255);
DROP TABLE IF EXISTS pplGrp;
CREATE TEMPORARY TABLE pplGrp(FirstName_tmp varchar(255), LastName_tmp varchar(255));
SELECT MAX(ChancesEnd) INTO rndmPlier FROM personlist WHERE country = tempCntry;
SET rndm1 = FLOOR((1+RAND() * (rndmPlier-1)));
SELECT p.name INTO firstN FROM personlist p WHERE country = tempCntry AND FirstName = 1 AND sex = 0 AND p.ChancesStart <= rndm1 AND p.ChancesEnd >= rndm1 LIMIT 1;
SET rndm2 = FLOOR((1+RAND() * (rndmPlier-1)));
SELECT p.name INTO lastN FROM personlist p WHERE country = tempCntry AND LastName = 1 AND sex = 0 AND p.ChancesStart <= rndm2 AND p.ChancesEnd >= rndm2 LIMIT 1;
INSERT INTO pplGrp (FirstName_tmp, LastName_tmp)values(firstN, lastN);
SELECT * FROM pplGrp;
END$$
DELIMITER ;

no return value of call procedure mysql

I have a procedure in mysql and it has 4 parameters as Input and 3 parameters of OUTPUT and one param of OUTPUT doesn't return nothing (null).
DELIMITER $$
drop procedure if exists `cierreMes`$$
create procedure cierreMes (in tarjeta varchar(100),in bancoBus varchar(100),in mes int,in anyo int, out total int, out nulas int, out erroneas int)
begin
declare stockActual int default 0;
declare cantidad int;
/*declare xcantidad,xnulas,xerroneas int;*/
declare entrada, salida int default 0;
declare total int default 0;
select stock
into stockActual
from almacen
where idProducto =
(select idProducto from productos where productos.banco = bancoBus and productos.plastico = tarjeta);
call entradasSalidas(tarjeta,bancoBus,mes,anyo,#ent,#sal);
set entrada = #ent;
set salida = #sal;
call obtenerMovimientosMes(tarjeta,bancoBus,mes,anyo,#cant,#nul,#err);
set cantidad = #cant;
set nulas = #nul;
set erroneas = #err;
set total =(stockActual + entrada) - (salida + cantidad);
select total;
end$$
DELIMITER ;
call cierreMes('4B MC','SANTANDER',3,2013, #total, #nulas, #erroneas);
select #total, #nulas, #erroneas;
When i do "call" #nulas and #erroneas return a value, but #total
nothing.
With select total, its works fine. but no returns a value, in this select : select #total, #nulas, #erroneas; #total is null.
You calculate total with this formula:
set total =(stockActual + entrada) - (salida + cantidad);
If one of the values used in the calculation is NULL, then total will be NULL.
I can see set statements for entrada, salida, and cantidad. But what is the value of stockActual?
It seems you are missing a statement that will set the value of stockActual.