I've created a procedure where I have 2 in parameters and 1 out parameter. However, I don't know why my output doesn't want to come out. I don't want to use a select statement. I would like to use the out parameter. Thanks.
create procedure quiz_totals(in q1 double unsigned, in q2 double unsigned, out p_total int)
begin
declare v_ceil_q1 int;
declare v_ceil_q2 int;
declare v_max int;
declare v_min int;
set v_ceil_q1 := ceiling(q1);
set v_ceil_q2 := ceiling(q2);
create table temp_tbl(t_scores int);
insert into temp_tbl(t_scores) values(v_ceil_q1), (v_ceil_q2));
select max(t_scores) into v_max from temp_tbl;
select min(t_scores) into v_min from temp_tbl;
set p_total := (v_ceil_q1) + (v_ceil_q2) + v_max - 2*v_min;
drop table temp_tbl;
end;
#
delimiter ;
call quiz_totals(23, 32.4, #total);
This is my output:
Query OK, 0 rows affected (0.02 sec)
No p_total ! Why?
You need a select, even if you don't want to...
SELECT #total;
If you wanna see what's inside !
Related
The following code is working properly. But when i am enabling the commented area (cursor), then the code showing error. Please help to fix the issue.
Scenario: The code allow some parameter. It will prepare the data in a table and then the cursor will take data from that table and output that data.
Same Parameter: call prGetInsuranceData_Multiple(2, 'Saroar,Ahmed', '20,30')
DELIMITER $$
USE `surokkha_db`$$
DROP PROCEDURE IF EXISTS `prGetInsuranceData_Multiple`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `prGetInsuranceData_Multiple`
(
PeopleToBeCovered INT,
IN NAME VARCHAR(4000),
IN AGE VARCHAR(200)
)
BEGIN
-- declare loop variables
DECLARE V_Name VARCHAR(255);
DECLARE V_AGE INT;
DECLARE X INT DEFAULT 0;
-- declare cursor variables
DECLARE Cur_Finished INTEGER DEFAULT 0;
DECLARE Cur_Name VARCHAR(255);
DECLARE Cur_Age INT;
-- create a table with comma separated values (Name with age in table format)
CREATE TEMPORARY TABLE TempCustomer
(
NAME VARCHAR(255),
AGE INT
);
CREATE TEMPORARY TABLE TempCustomer1
(
NAME VARCHAR(255),
AGE INT
);
SET X = 1;
BEGIN
WHILE X <= PeopleToBeCovered DO
SET V_Name = SUBSTRING_INDEX(SUBSTRING_INDEX(NAME,',',X),',',-1);
SET V_Age = SUBSTRING_INDEX(SUBSTRING_INDEX(AGE,',',X),',',-1);
SET X = X + 1;
INSERT INTO TempCustomer VALUES(V_Name, V_Age);
END WHILE;
END;
/*
-- declare cursor
DECLARE cur_NameWithAge
CURSOR FOR
SELECT NAME, AGE FROM TempCustomer;
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
OPEN cur_NameWithAge;
GetNameWithAge: LOOP
FETCH cur_NameWithAge INTO Cur_Name, Cur_Age;
IF finished = 1 THEN
LEAVE GetNameWithAge;
END IF;
-- get data and insert into table
INSERT INTO TempCustomer1 VALUES(Cur_Name, Cur_Age);
END LOOP GetNameWithAge;
CLOSE cur_NameWithAge;
*/
SELECT * FROM TempCustomer;
-- as after setting cursor the data is not needed, thats why drop the tables
DROP TEMPORARY TABLE TempCustomer;
DROP TEMPORARY TABLE TempCustomer1;
END$$
DELIMITER ;
You need to pout the loop and its declarations in a BEGIn ENd
Still i needed to reprogram the hole thing, because your code threw error, that i couldn't find
create procedure prGetInsuranceData_Multiple(
IN PeopleToBeCovered INT,
IN _NAME VARCHAR(4000),
IN _AGE VARCHAR(200))
begin
DECLARE V_Name VARCHAR(255);
DECLARE V_AGE INT;
DECLARE X INT DEFAULT 0;
drop temporary table if exists TempCustomer;
drop temporary table if exists TempCustomer1;
create temporary table TempCustomer( NAME VARCHAR(255),AGE int );
create temporary table TempCustomer1(NAME VARCHAR(255),AGE int);
SET X = 1;
BEGIN
WHILE X <= PeopleToBeCovered DO
SET V_Name = SUBSTRING_INDEX(SUBSTRING_INDEX(_NAME,',',X),',',-1);
SET V_Age = SUBSTRING_INDEX(SUBSTRING_INDEX(_AGE,',',X),',',-1);
SET X = X + 1;
INSERT INTO TempCustomer VALUES(V_Name,V_Age);
END WHILE;
END;
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE v_id int ;
DECLARE v_name varchar(255);
declare cur_NameWithAge cursor for select NAME,AGE from TempCustomer;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
open cur_NameWithAge;
GetNameWithAge: LOOP
fetch cur_NameWithAge into v_name,v_id;
IF finished = 1 THEN
LEAVE GetNameWithAge;
END IF;
INSERT INTO TempCustomer1 VALUES (v_name,v_id);
END LOOP GetNameWithAge;
CLOSE cur_NameWithAge;
END;
select * FROM TempCustomer;
DROP TEMPORARY TABLE TempCustomer1;
DROP TEMPORARY TABLE TempCustomer1;
end
call prGetInsuranceData_Multiple(2, 'Saroar,Ahmed', '20,30')
NAME | AGE
:----- | --:
Saroar | 20
Ahmed | 30
db<>fiddle here
I have an issue with my SQL procedure.
MySQLWorkbench advise me that it miss 'end' to my first SET, but not for the second. I don't know why.
DELIMITER $
drop procedure if exists pay10percent$
create procedure pay10percent(IN montant decimal(9,2),IN idResa INT(5))
begin
declare circuitid INT;
SET circuitid = (
SELECT IDCIRCUIT
FROM RESERVATION
WHERE IDRESERVATION=idResa
);
declare montantCircuit decimal(9,2);
SET montantCircuit = (SELECT PRIX FROM CIRCUIT WHERE IDCIRCUIT=circuitid);
end;
$
DELIMITER ;
Thanks.
You must declare all the variables before using SET. Alternatively, you can drop SET and use that subquery as a default value:
declare circuitid INT DEFAULT (
SELECT IDCIRCUIT
FROM RESERVATION
WHERE IDRESERVATION=idResa
);
I'm trying to create a stored procedure using Mysql workbench what i want is to know how many rows affected without display all the rows when i remove my selection no rows affected. can anyone help me please?
This my procedure :
DROP PROCEDURE IF EXISTS GetAllProducts;
SELECT * FROM csii;
DELIMITER |
CREATE PROCEDURE GetAllProducts ()
BEGIN
DECLARE csii_id INT;
DECLARE csii_fk_cc INT;
DECLARE csii_s VARCHAR(255);
DECLARE csi_p DECIMAL;
DECLARE csi_c_a DATE;
DECLARE csi_o_p DECIMAL;
DECLARE csi_shi_c DECIMAL;
DECLARE csi_xc_s VARCHAR(20);
DECLARE csi_wei VARCHAR(20);
DECLARE csi_fk_c_at INT;
DECLARE csi_sta enum('a','i','d');
SELECT
id, fk_cc, s, p, c_a, o_p, shi_c, xc_s, wei, fk_c_at, sta
INTO
csii_id, csii_fk_cc, csii_s, csi_p, csi_c_a, csi_o_p, csi_shi_c,
csi_xc_s, csi_wei, csi_fk_c_at, csi_sta
FROM
csii
WHERE
csi_sta = sta AND csi_sta = 'a';
END|
DELIMITER ;
Many thanks for any help.
You should just be able to add SELECT ROW_COUNT(); to the end of your query.
I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;
How do you declare variable and set a value which is a return from a query on them later.
Sample Stored Procedure:
DELIMITER $$
CREATE PROCEDURE `sampledb`.`SetVariableEx`()
BEGIN
-- declare variable
DECLARE xVarA INT;
DECLARE xVarB INT;
-- in this line, i would like to set a value on xVarA which is a COUNT
-- of record from table SINGLETABLE
-- i am getting error on this line.
SELECT xVarA := COUNT(*) FROM SingleTable;
-- the value of xVarA is added by 1 and set it to xVarB
SET xVarB = xVarA + 1;
-- insert the value of xVarB to the table SINGLETABLE
INSERT INTO SingleTable(SingleColumn) VALUES (xVarB);
-- lastly, display all records.
SELECT * FROM SingleTable;
END$$
DELIMITER ;
how would i do that?
Try the following:
SET xVarA := (SELECT COUNT(*) FROM SingleTable);
However, for this example, have you considered using an auto auto-incrementing value, rather than managing the value yourself?