Mysql : Error in trigger - mysql

This is the trigger im trying to add. But here is the error message:
#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 'create trigger tgr_insert before insert on `nuss_order` for
each row ' at line 2 .
delimiter $$
drop trigger if exists tgr_insert
create trigger tgr_insert before insert on `nuss_order`
for each row
begin
declare a int;declare b varchar(14);declare d varchar(8);
set a = (select `id` from `nuss_order` where `id` = new.id);
set d = (select `date` from `nuss_order` where `date` = new.date);
if (a>=100) then
if(a>=1000) then
set b = 'BX' + d + cast(a as varchar);
else
b = 'BX' + d + concat('0',cast(a as varchar));
end if;
else
if(a>=10) then
b = 'BX' + d + concat('00',cast(a as varchar));
else
b = 'BX' + d + concat('000',cast(a as varchar));
end if;
end if;
set new.oid = b;
end
I can't find the error, if someone could help me it would be very nice. Thanks

Related

Error mysql syntax trigger

According phpmyadmin, I have a syntax error in this trigger :
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
m_id_d := 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d == 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE TABLE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
The error is :
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 ':= 0;
What is the error ? I don't understand what I am doing wrong..
Thanks.
There are several syntax errors in your trigger:
declare a variable, you should invoke it with set;
IF expression can not use ==, just one equal is ok;
update syntax only need to specify table name like UPDATE table_d SET ....
So please try following trigger:
delimiter $$
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
set m_id_d = 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d = 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
$$

ERROR 1064 (42000): check the manual that corresponds to your MySQL server version for the right syntax to use near 'END'

here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
ERROR 1064 (42000): 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 'END' at line 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;

MySQL Stored Procedure Issue #1064

This little one gives me syntax problem, i cant solve that i'll appreciate that.
DELIMITER $$
CREATE procedure kullanicibilgicek
(
p_kadi varchar(50)
)
begin
declare v_sonuc INT;
if(CHAR_LENGTH(RTRIM(p_kadi)) > 4)
then
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi)
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi;
else
set v_sonuc = 0;
select v_sonuc;
end if;
else
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where id = Cast(p_kadi as int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where id = cast(p_kadi as int);
else
set v_sonuc = 0;
select v_sonuc;
end if;
end if;
END;
$$
DELIMITER ;
#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 'int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1' at line 18

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 ;

syntax error on updating two tables with one trigger

hi i'm trying to update two different tables with one trigger but i keep getting syntax error and i'm not sure what i'm doing wrong
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 'UPDATE specialitys s
JOIN speciality_objects o
ON s.id = o.speciality' at line 2
triggers works fine if i just update one table but i cant put them both in one trigger
here is my trigger
DELIMITER $$
CREATE TRIGGER after_user_type_or_status_change
AFTER UPDATE
ON users
FOR EACH ROW
BEGIN
UPDATE stats s
SET
ee_counter =
CASE
WHEN NEW.type= 1 THEN ee_counter + 1
WHEN NEW.type= 3 && OLD.type = 2 THEN ee_counter + 1
WHEN OLD.type= 1 && NEW.type != 3 THEN ee_counter - 1
ELSE ee_counter
END ,
er_counter =
CASE
WHEN NEW.type= 2 THEN er_counter + 1
WHEN NEW.type= 3 && OLD.type = 1 THEN er_counter + 1
WHEN OLD.type= 2 && NEW.type != 3 THEN er_counter - 1
ELSE er_counter
END
WHERE
s.id = 1;
END $$
BEGIN
UPDATE specialitys sp
JOIN speciality_objects o
ON sp.id = o.speciality_id
JOIN users u
ON o.user_id = u.id
SET
counter =
CASE
WHEN NEW.status = 1 THEN counter + 1
ELSE counter
END
WHERE
sp.id = o.speciality_id ;
END $$
DELIMITER ;
The END $$ in the middle is ending the trigger definition. Remove it and the following BEGIN.