I am inserting the records in mysql database using while loop.I want to chcek the check atleast one record is inserted or not. I tried below code but ROW_COUNT() give me success, if the record is not inserted.
DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE res varchar(255);
WHILE count < 10 DO
/**Sql statement**/
SET count = count + 1;
END WHILE;
IF ROW_COUNT() > 0 THEN
SET res = 'success';
ELSE
SET res = 'failure';
END IF;
SELECT res;
END$$
DELIMITER ;
Related
delimiter $$
create procedure mergeData()
begin
declare counter, rowws int default 0;
set rowws = (select count(*) from new_branches);
begin
while counter < rowws do
if new_branches.branch_no != branch_mstr.branch_no then
insert into branch_mstr value(new_branches.branch_no, new_branches.name);
end while;
end;
end$$
delimiter ;
Why these crosses are showing, I am using MySQL and new to the PL/SQL. Asking for the help
Is it possible to insert a control on the data that insert in a table? In a way that satisfies a specific condition provided, otherwise not
drop function Presente;
delimiter //
create function Presente( Persona_Da_Cercare varchar(16), Data_Legge date) returns int
begin
declare risultato int;
select count(*) into risultato
from PARLAMENTARI_IN_CARICA as pic
where Persona_Da_Cercare = pic.Persona and data_Legge >= pic.Data_Eletto;
if risultato = 0
then
select count(*) into risultato
from STORICO_PARLAMENTARI as sp
where Persona_Da_Cercare = sp.Persona and data_Legge >= sp.Data_Inizio and data_Legge <= sp.Data_Fine;
end if;
return risultato;
end//
set #ultima_legge bigint;
set #ultimo_parlamentare varchar(16);
set #data_legge date;
drop trigger if exists Ultimo_Voto;
delimiter $$
create trigger Ultimo_Voto before insert on VOTO_PALESE
for each row
begin
set #ultima_legge = new.Legge;
set #ultimo_parlamentare = new.parlamentare;
set #data_legge = (select Data_Approvazione from LEGGI where Cod_Legge = new.Legge);
end $$
drop trigger if exists Controllo_Voto_Palese;
delimiter $$
create trigger Controllo_Voto_Palese after insert on VOTO_PALESE
for each row
BEGIN
IF Presente(#ultimo_parlamentare, #data_legge) = 0 THEN
delete from VOTO_PALESE where Legge = #ultima_legge AND Parlamentare = #ultimo_parlamentare;
END IF;
END $$
insert into VOTO_PALESE(Legge, Parlamentare, Voto) values(770, 'MSRMRC01C14L378X', 'astenuto');
when I run the code it gives me this error
Error Code: 1442. Can't update table 'voto_palese' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
I don't understand where the problem is
I have created a Trigger after INSERT on the table AcademicYearTermLevel which uses a cursor value fetched from another query. I want to use each cursor value in a while loop and insert rows into table SubjectYearTermLevel.
DROP TRIGGER IF EXISTS after_academicyearterm_insert
DELIMITER $$
CREATE TRIGGER after_academicyearterm_insert
AFTER INSERT
ON AcademicYearTerm
FOR EACH ROW
BEGIN
DECLARE temp INT;
DECLARE subj_done, form_done BOOLEAN DEFAULT FALSE;
DECLARE s_id INT;
DECLARE curSubject CURSOR FOR SELECT subject_id FROM Subject;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET subj_done = TRUE;
SET temp = 1;
OPEN curSubject;
cur_subject_loop: LOOP
FETCH FROM curSubject INTO s_id;
IF subj_done THEN
CLOSE curSubject;
LEAVE cur_subject_loop;
END IF;
WHILE temp <= 6 DO
INSERT INTO SubjectYearTermLevel VALUES (NULL, s_id, NEW.yearTerm_id, temp);
SET temp = temp + 1;
END WHILE
END LOOP cur_subject_loop;
END$$
DELIMITER ;
However the problem is that the cursor only seems to fetch one value from the SELECT query
Silly mistake, I SET temp = 1; outside the FETCH, and hence after the first cursor value, the while loop never ran.
-- SET temp = 1;
OPEN curSubject;
cur_subject_loop: LOOP
FETCH FROM curSubject INTO s_id;
SET temp = 1;
IF subj_done THEN
CLOSE curSubject;
LEAVE cur_subject_loop;
END IF;
1_to_6_temp: WHILE temp <= 6 DO
INSERT INTO SubjectYearTermLevel VALUES (NULL, s_id, NEW.yearTerm_id, temp, -1);
SET temp = temp + 1;
END WHILE 1_to_6_temp;
-- CLOSE curSubject;
END LOOP cur_subject_loop;
i am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;
SET SQL_SAFE_UPDATES = 0;
use my_database;
DELIMITER $$
DROP PROCEDURE IF EXISTS Comit $$
CREATE PROCEDURE Comit ()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE ids INT;
DECLARE leftChilds INT;
DECLARE cur CURSOR FOR SELECT id FROM user;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
ins_loop: LOOP
FETCH cur INTO ids;
IF done THEN
LEAVE ins_loop;
END IF;
SET leftChilds = ( SELECT turnoverBalance FROM user WHERE proposer = ids AND side = 'left' LIMIT 1 );
INSERT INTO log(`log`) VALUES ( leftChilds );
END LOOP;
CLOSE cur;
END $$
When i call the procedure call Comit(); that return me this error :
1048 - Column 'log' cannot be null
Your subquery is generating NULL values, probably because there is no match on the proposer condition. Of course, your data could also have NULL values for turnoverBalance in user or rows with no 'left' side.
In any case, why are you using a cursor for something that is easily done as a single query? Something like this can replace all the logic:
INSERT INTO log(log)
SELECT turoverBalance
FROM user
WHERE proposer IN (SELECT id FROM user) AND side = 'left')
GROUP BY proposer;
First, its are stored procedure, not a trigger.
Check what your set leftChilds query returns, it should return some value, run it individually.
You can Check in stored procedure
If(leftChilds not NULL)
insert into log('log') values (leftChilds)