MySQL Assign Return Value to Variable - mysql

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?

Related

Update the same row after insert Inside the trigger

I have to use a trigger that will update the inserted row. Following is my trigger.
DELIMITER $$
USE `kikan_db`$$
CREATE TRIGGER `t_trigger` AFTER INSERT ON `t_order`
FOR EACH ROW BEGIN
Declare kb_rate int;
Declare kb_amount int;
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);
SELECT kickback_rate INTO kb_rate from m_customer where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
UPDATE `t_order` SET `t_order`.kickback_amount = kb_amount where `t_order`.order_no = NEW.order_no;
END;
$$
DELIMITER ;
Getting error at update.
It is mandatory for me to use AFTER instead of BEFORE type of trigger
Instead of using only after insert/update trigger, (1) you can change the value of virtual new row on before insert/update :
Declare kb_rate int;
Declare kb_amount int;
SELECT kickback_rate INTO kb_rate from m_customer
where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
SET new.kickback_amount = kb_amount;
(2)And execute the other SQL statements in a after insert/update trigger:
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);

Assigning and modifying MySQL query to variable

This is my first procedure in MySQL and I am trying to take the ID column from my table, store it into a variable and then add 1 to it and then update the table with the new value. When I call myFirstProcedure() it sets all of the id values to 6 rather than increasing each by 1. How do I code this correctly?
DELIMITER //
CREATE PROCEDURE myFirstProcedure()
BEGIN
DECLARE IdValue INT DEFAULT 0;
SELECT COUNT(*) INTO IdValue
FROM new_table;
UPDATE new_table
SET ID = IdValue +1;
END//
DELIMITER ;
That is because you are setting all the values to the same value. You can do this by incrementing the variable in the stored procedure:
DELIMITER //
CREATE PROCEDURE myFirstProcedure()
BEGIN
DECLARE v_maxid;
SELECT COUNT(*) INTO v_maxid
FROM new_table;
UPDATE new_table
SET ID = (v_maxid := v_maxid + 1);
END//
DELIMITER ;
Note that COUNT(*) will return 0 if the table is empty, so there is no problem with NULL values.

UPDATE table id and its constraints MYSQL

I Have a Database in mysql, i have a principal table that use a column name cedula VARCHAR (50), the column have characters, but i want to change it with a consecutive number. I tried with this:
SET #a:=1;
UPDATE bdpiiad2.tpersona
SET cedula=(CONVERT(#a:=#a+1, CHAR(50)))
But i have 2 problems, first doesn't change, and second I'm not sure if change the other ids in the others tables that have a reference.
try using this
SET #a:=1;
UPDATE bdpiiad2.tpersona
SET cedula=(CAST(#a:=#a+1 as CHAR));
to update all you need to create a procedure like this
DELIMITER $$
USE `bdpiiad2`$$
DROP PROCEDURE IF EXISTS bdpiiad2.`up`$$
CREATE PROCEDURE `up`()
BEGIN
DECLARE max_cnt INT;
DECLARE cnt INT;
SET cnt=1;
UPDATE bdpiiad2.tpersona SET cedula='0';
SELECT COUNT(*) INTO max_cnt FROM bdpiiad2.tpersona;
WHILE cnt<=max_cnt DO
UPDATE bdpiiad2.tpersona
SET cedula=(CAST(cnt AS CHAR))
WHERE cedula='0' LIMIT 1;
SET cnt=cnt+1;
END WHILE;
END$$
DELIMITER ;
and then call it like this
CALL bdpiiad2.up();

mysql out parameter for procedure not showing

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 !

MySQL Procedure random number generating

I'm writing my first MySQL procedure. I try to generate a random number and update a value in a table with this number, but only if it doesn't exists already (due to unique constraint). My procedure looks like this:
create procedure generaterc()
begin
declare _rc char;
declare _id int;
set _id = 1;
while _id < ((select count(*) from patient) - 1) do
begin
set _rc = cast(FLOOR(1000000000 + (RAND() * 8999999999)) AS char);
select _rc;
if not exists(select * from patient where patient.rc = _rc) then
update patient set rc=_rc where id=_id;
set _id=_id+1;
end if;
end;
end while;
end
I got this error when executing the procedure: Data truncation: Data too long for column '_rc' at row 8. My rc column is varchar(255), but I guess this isn't the core of the problem. Any suggestions?
Thank you very much.
Instead of
declare _rc char;
try:
declare _rc varchar(255);
Currently _rc can only hold a single character, which is not enough to store your number.
However, for your use case you may like to take a look at the uuid_short() function. It generates a large random number that is guaranteed to be unique (subject to some rules). This way you can replace your procedure with the single statement:
update patient set rc = uuid_short();