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.
Related
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
DELIMITER //
CREATE OR REPLACE PROCEDURE GET_USER_PNTS(USER_ID INT , PNTS INT, QNT INT)
BEGIN
DECLARE x INT DEFAULT 1;
DECLARE TEMP_GIFT_ID INT;
UPDATE USR_PNT_SUMM SET USD_PNTS = USD_PNTS + PNTS WHERE USER_ID = 1;
COMMIT;
END //
DELIMITER ;
The above stored procedure updates two rows - one for user_id = 1 and the other one for userid 0. I dont understand why!
This is how I call the stored procedure -
CALL GET_USER_PNTS(1, 1, 1)
Please let me know why the user_id 0 is also getting updated.
P.S
1. I am using MariaDB.
2. UserID 0 is what I had manually added in the table. In pratice there won't be any 0 user_id. But even then, the row should not have been updated.
Please rename your parameters:
CREATE OR REPLACE PROCEDURE GET_USER_PNTS(L_USER_ID INT , L_PNTS INT, L_QNT INT)
BEGIN
DECLARE x INT DEFAULT 1;
DECLARE TEMP_GIFT_ID INT;
UPDATE USR_PNT_SUMM SET USD_PNTS = USD_PNTS + L_PNTS WHERE USER_ID = L_USER_ID;
COMMIT;
END //
Probably USER_ID = USER_ID is treated as true.
CREATE DEFINER=`root`#`localhost` PROCEDURE `test4`()
BEGIN
declare GroupArray text;
DECLARE i INT DEFAULT 0;
DECLARE loopcount INT DEFAULT 0;
declare GroupId varchar (5);
set GroupArray=(select group_concat(distinct groupId) from EventList_View);
SET loopcount = (SELECT LENGTH(GroupArray) -
LENGTH(REPLACE(GroupArray,',', '')));
select loopcount;
WHILE i <= loopcount DO
SET i = i + 1;
SET GroupId = (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(GroupArray,
',', i),',', -1));
select GroupId;
END WHILE;
END
this is my query i am trying to apply while loop and i want get all groupId but GroupArray [1,2,3]i am getting value but in loop count i am getting 0 can any one please tell me where am doing wrong . why i am unable to get loop count null and why i am not getting value 1,2,3 for group
Here's my Stored Procedure:
DELIMITER //
CREATE PROCEDURE facturar(codigo int(6),fecha date,cedula VARCHAR(9),cantidad int(3),peso VARCHAR(10),precio varchar(10))
BEGIN
DECLARE P float;
Declare total float;
set P=0;
set total=0;
insert into factura VALUES(ID,fecha,cedula,cantidad, peso, `precio`);
set P=peso*4;
if P between 2000 and 10000 then SET precio= (P*0.05)+(P*0.12)+P;
elseif P > 10000 then set precio= (P*0.05)+(P*0.10)+(P*0.12)+P;
else set precio= (P*0.12)+P;
end if;
set total = precio;
select concat("Total de envio=", total);
END;
//
When i make the call:
call facturar (NULL,'2016/01/13', 'v18834415', '2', '96400', '');
It shows the result:
Total de envió=489712
But, once I take a look at the table the "precio" is in white while the other values are filled.
You are setting precio as a column in the insert. Then you do some calculations, and never do anything with the values.
I strongly encourage two of things:
indicate the difference between variables and columns by using a prefix for variables
list all columns for insert
In your case, I think you need to move the insert after you set precio:
CREATE PROCEDURE facturar(
in_codigo int(6),
in_fecha date,
in_cedula VARCHAR(9),
in_cantidad int(3),
in_peso VARCHAR(10),
in_precio varchar(10)
)
BEGIN
DECLARE #P float;
Declare #total float;
set #P = 0;
set #total = 0;
set #P = in_peso*4;
if #P between 2000 and 10000 then SET in_precio= (#P*0.05)+(#P*0.12)+#P;
elseif #P > 10000 then set in_precio= (#P*0.05)+(#P*0.10)+(#P*0.12)+#P;
else set in_precio = (#P*0.12)+#P;
end if;
set #total = in_precio;
insert into factura( . . . )
VALUES(in_ID, in_fecha, in_cedula, in_cantidad, in_peso, in_precio);
select concat('Total de envio=', #total);
END;
You need to execute insert query after setting the value.
DELIMITER //
CREATE PROCEDURE facturar(codigo int(6),fecha date,cedula VARCHAR(9),cantidad int(3),peso VARCHAR(10),precio varchar(10))
BEGIN
DECLARE P float;
Declare total float;
set P=0;
set total=0;
set P=peso*4;
if P between 2000 and 10000 then SET precio= (P*0.05)+(P*0.12)+P;
elseif P > 10000 then set precio= (P*0.05)+(P*0.10)+(P*0.12)+P;
else set precio= (P*0.12)+P;
end if;
set total = precio;
select concat("Total de envio=", total);
insert into factura VALUES(ID,fecha,cedula,cantidad, peso, total);
END;
//
use total variable in insert query instead of backquoted value
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 ;