Error in mysql trigger and update - mysql

I have the next trigger:
CREATE
TRIGGER t_call_status
AFTER INSERT
ON tblcdr FOR EACH ROW
BEGIN
DECLARE lastid integer;
DECLARE nseg integer;
SET lastid = LAST_INSERT_ID();
SET nseg = 120;
IF ((origen = 'ANSWERED' && destino = 'ANSWERED') && (billsec_origen >= nseg && disposition_destino >= nseg)) THEN
update tblcdr set call_status = 3 where id = lastid;
ELSE IF (origen = 'ANSWERED' && destino = 'ANSWERED') THEN
update tblcdr set call_status = 2 where id = lastid;
ELSE IF (origen = 'ANSWERED' && destino <> 'ANSWERED') THEN
update tblcdr set call_status = 1 where id = lastid;
ELSE IF (origen <> 'ANSWERED' && destino <> 'ANSWERED') THEN
update tblcdr set call_status = 0 where id = lastid;
ELSE
update tblcdr set call_status = 0 where id = lastid;
END IF;
END;
But, i got the next error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '' at line 7
How can I fix it?
Greetings.

Related

how to use if condition in mysql with status = 2

create trigger with status with passed with status = 2 and I need to create a tigger after update row second time when del_status = 2
CREATE TRIGGER `exchange_log_update` AFTER UPDATE ON `exchange`
FOR EACH ROW INSERT
IF NEW.del_status = 2
INTO
exchange_log
SET
client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at
END IF
END
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF NEW.del_status
INTO
exchange_log
SET
client_id = NEW.client_id,
ex' at line 3
try this
CREATE TRIGGER `exchange_log_update_new` AFTER UPDATE ON `exchange`
FOR EACH ROW BEGIN
IF (NEW.del_status = 2)
THEN
INSERT INTO `exchange_log`
values(client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at);
END IF;
END

MySQL Function IF() do not return the value that i declare

i want my function to return "INC" when put "INC" inside the function parameters
SELECT getEquivalentGrade('INC');
Output: INC
DELIMITER $$
USE `nmsc_scholarship_db`$$
DROP FUNCTION IF EXISTS `getEquivalentGrade`$$
CREATE DEFINER =`root`#`localhost` FUNCTION `getEquivalentGrade`(grade CHAR(5))
RETURNS CHAR(5)
CHARSET latin1
BEGIN
DECLARE equivalent_grade CHAR(5);
IF (grade > 100)
THEN
SET equivalent_grade = '1';
ELSEIF (grade < 69)
THEN
SET equivalent_grade = '5';
ELSEIF (grade = 'INC')
THEN
SET equivalent_grade = grade;
ELSE
SET equivalent_grade = (SELECT equivalent_grade.`equivgrade`
FROM equivalent_grade
WHERE equivalent_grade.`fromgrade` <= grade AND equivalent_grade.`tograde` >= grade);
END IF;
RETURN equivalent_grade;
END$$
DELIMITER ;
now, if i will execute this function, this is what i get.
SELECT getEquivalentGrade('INC');
Output: 5
You should move the INC to the TOP
IF (grade='INC') THEN
SET equivalent_grade = grade;
ELSEIF (grade > 100) THEN
SET equivalent_grade = '1';
ELSEIF (grade < 69) THEN
SET equivalent_grade = '5';
ELSE
SET equivalent_grade = (SELECT equivalent_grade.`equivgrade` FROM equivalent_grade WHERE equivalent_grade.`fromgrade`<= grade AND equivalent_grade.`tograde`>= grade);
And you should research how IFELSE work.
Condition was working like it will stop/goto ENDIF.. IF** the condition true was meet, or they will keep finding till the latest IF

Trigger Update - 2 Tables

I need a trigger for when I Fize hum insert into the table " reserve" automatically add in Table Room The Current State .
A major issue is that I need to check if reserva.id E equal to quarto.id .
How can I check the condition WHERE The Field And Correspondent ?
Here's Some failed attempts codes .
CREATE TRIGGER OcuparQuarto
AFTER UPDATE OF id ON reserva
FOR EACH ROW
BEGIN
UPDATE quarto
SET status='0'
WHERE id=NEW.id;
END;
or
Delimiter |
create trigger OcuparQuarto
after insert on reserva
for each ROW
UPDATE quarto
SET quarto.status = 0
FROM quarto
INNER JOIN reserva ON quarto.id = reserva.id
END;
|
delimiter ;
begin
if (new.status = 0) then update quarto set status = 0 where quarto.id
= new.quarto_id;
elseif (new.status = 1) then update quarto set status = 1 where
quarto.id = new.quarto_id;
elseif (new.status = 2) then update quarto set status = 0 where
quarto.id = new.quarto_id;
elseif (new.status = 3) then update quarto set status = 1 where
quarto.id = new.quarto_id;
end if; end
This Works! Thanks!

Syntax Issue With MySQL Trigger

DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand
WHERE (tx_no = NEW.tx_no AND
input_area = NEW.input_area AND
tx_line_no = NEW.tx_line_no AND
bill_no <> "") OR
(tx_no = NEW.tx_no AND
bill_no <> "")
LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap
(tx_no, input_area, tx_line_no, bill_cust_code, bill_no)
VALUES
(NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END;$$
It returns an error
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$$' at line 14"
Can you please tell me, what is wrong with code..
DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand WHERE (tx_no = NEW.tx_no AND input_area = NEW.input_area AND tx_line_no = NEW.tx_line_no AND bill_no <> "") OR (tx_no = NEW.tx_no AND bill_no <> "") LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap (tx_no, input_area, tx_line_no, bill_cust_code, bill_no) VALUES (NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END$$
DELIMITER ;

Unexpected IDENT_QUOTED, expecting ( in nested IF/ELSE IF statement

if v_test_id IS NOT NULL AND exists (select test_id from physicians where test_id = v_test_id) then
select 'phys_id' = -1;
elseif
if v_addl_language1 = '' THEN SELECT v_addl_language1 = NULL;
if v_addl_language2 = '' THEN SELECT v_addl_language2 = NULL;
if v_addl_language3 = '' THEN SELECT v_addl_language3 = NULL;
The error is occuring at if v_addl_language1...
I've tooled with adding parentheticals around the parameter of the if statements but that doesn't seem to fix it. Any ideas?
You miss END IF; statement for every IF
The sintaxe is IF expression THEN commands; ELSEIF anotherExpression THEN commands; ELSE commands; END IF;
So your code at the elseif part should be:
ELSEIF someexpression THEN
if v_addl_language1 = '' THEN SELECT v_addl_language1 = NULL; end if;
if v_addl_language2 = '' THEN SELECT v_addl_language2 = NULL; end if;
if v_addl_language3 = '' THEN SELECT v_addl_language3 = NULL; end if;
END IF;