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 ;
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
[
For the cname='Liam', there are 3 card_id
i.e. 87260101, 87260153, 87501026
We want to find out how many extra card_id this person has, means we want output=2.
For the cname='Elizabeth', there are 2 card_id.
i.e.87501022, 87501000 and we want an output of 1.
For the others who only has one card_id which correspond to an empty loss_report_date,
count(loss_report_date)=0, the output=0
For the code below, we get the error code=1111, invalid use of group function, but the store procedure does not report any error when we run procedure alone.
The DBMS we are using is Innodb.
DELIMITER $$
CREATE PROCEDURE Gettransaction(IN cname varchar(50), OUT cnt int)
BEGIN
#select card_id if there is no old_id, if t1.card_id=t2.old_id then use card_id
DECLARE maxcardid char(8);
DECLARE cnt int default 1;
DECLARE nextMax int;
set maxcardid = (select max(card_id) from card
group by cname);
WHILE cname in (select cname from card where count(loss_report_date)>=1)
DO
set nextMax = (select max(card_id)
from card
where card_id < maxcardid);
set cnt = cnt + 1;
set maxcardid = nextMax;
END WHILE;
END$$
DELIMITER ;
call Gettransaction('Liam', #output);
select #output
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.
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.
I have some troubles with DECLARE, CALL function and a IF settelment. What i have so far is:
CREATE PROCEDURE number_of_projects(project_name VARCHAR)
BEGIN
DECLARE variable INT;
SET variable = 4;
SELECT variable;
SELECT count(project_id) FROM atm_projects WHERE project_name = variable;
END
IF number_of_projects("PROJECT NAME") = 0
THEN
INSERT INTO atm_projects(project_id,project_name,added_from_mti)
VALUES (project_id,'PROJECT NAME',1)
ENDIF
The main goal is to insert a row into a table where project_name is not duplicated.
I could change project_name to an UNIQUE key but please tell me what is wrong with my code, an how can i fix this?
I need to learn how a PROCEDURE, CALL procedure, IF works.
try:
CREATE PROCEDURE number_of_projects(project_name VARCHAR(255))
BEGIN
DECLARE var_project_no INT;
SET var_project_no = 0;
SELECT var_project_no;
SELECT count(project_id) INTO var_project_no FROM atm_projects WHERE var_project_name = 4;
IF var_project_no = 0
THEN
INSERT INTO atm_projects(project_id,project_name,added_from_mti)
VALUES (project_id,'PROJECT NAME',1)
END IF;
END;
You have declared project_name to varchar without specifying the length. Change it to varchar(100)
CREATE PROCEDURE number_of_projects(project_name VARCHAR(255))
BEGIN
if NOT exists(SELECT Top 1 1 FROM atm_projects WHERE var_project_name = 4)
BEGIN
INSERT INTO atm_projects(project_id,project_name,added_from_mti)
Select project_id,project_name ,1
END
END;