Syntax Issue With MySQL Trigger - mysql

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 ;

Related

MYSQL TRIGGER with DECLARE value

I want to set value using declare on trigger. The trigger is as follow:
CREATE DEFINER=`root`#`localhost` TRIGGER `update_queue_after_insert` AFTER INSERT ON `encounter_note` FOR EACH ROW DECLARE is_exist INT;
SET is_exist = ( SELECT count(*) FROM practice_last_updated_module WHERE practice_id = NEW.practice_id );
IF NEW.enc_source = 'OP' THEN
UPDATE practice_queue_list PQL SET
PQL.vital_check = IF (NEW.vs_weight <> 0 OR NEW.vs_height <> 0 OR NEW. vs_temperature <> 0 OR LENGTH(NEW.vs_blood_pressure) > 0 <> NEW.vs_pulse <> 0 OR NEW.vs_respiration <> 0, 1, 0)
WHERE PQL.encounter_id = NEW.id AND PQL.practice_place_id = NEW.practice_id;
END IF;
IF is_exist > 0 THEN
UPDATE practice_last_updated_module SET encounter = UNIX_TIMESTAMP(NOW()) where practice_id = NEW.practice_id;
ELSE:
INSERT INTO practice_last_updated_module (practice_id, encounter) VALUES (NEW.practice_id, UNIX_TIMESTAMP(NOW()));
END IF;"
But it returns error on saving
The following query has failed: "CREATE DEFINER=root#localhost
TRIGGER update_queue_after_insert AFTER INSERT ON encounter_note
FOR EACH ROW DECLARE is_exist INT; SET is_exist = ( SELECT count(*)
FROM practice_last_updated_module WHERE
What's wrong with the statement?
Use 13.6.1 BEGIN ... END Compound-Statement Syntax.
Try (maybe you need to use DELIMITER):
DELIMITER //
CREATE TRIGGER `update_queue_after_insert` AFTER INSERT ON `encounter_note`
FOR EACH ROW
BEGIN -- <- BEGIN
DECLARE is_exist INT;
SET is_exist = ( SELECT count(*) FROM practice_last_updated_module WHERE practice_id = NEW.practice_id );
IF NEW.enc_source = 'OP' THEN
UPDATE practice_queue_list PQL
SET PQL.vital_check = IF (NEW.vs_weight <> 0 OR NEW.vs_height <> 0 OR NEW.vs_temperature <> 0 OR LENGTH(NEW.vs_blood_pressure) > 0 <> NEW.vs_pulse <> 0 OR NEW.vs_respiration <> 0, 1, 0)
WHERE PQL.encounter_id = NEW.id AND PQL.practice_place_id = NEW.practice_id;
END IF;
IF is_exist > 0 THEN
UPDATE practice_last_updated_module SET encounter = UNIX_TIMESTAMP(NOW()) where practice_id = NEW.practice_id;
-- ELSE:
ELSE
INSERT INTO practice_last_updated_module (practice_id, encounter) VALUES (NEW.practice_id, UNIX_TIMESTAMP(NOW()));
END IF;
END// -- <- END
DELIMITER ;

What does "The following query failed" ";"" mean in MySQL?

While trying to update a trigger, MySQL tells me the query ";" failed. How is ";" even a query in MySQL's view is beyond me.
The exact message is:
The following query has failed: ";" MySQL said: #1065 - Query was empty
Here's the new trigger (AFTER INSERT):
BEGIN
DECLARE vIdPlacet VARCHAR(40);
DECLARE vTypeTravaux VARCHAR(32);
DECLARE vEssence VARCHAR(3) DEFAULT '-';
DECLARE vClasseHau VARCHAR(5) DEFAULT '-';
DECLARE vNoMesurag int;
DECLARE new_id_parcelle INT UNSIGNED DEFAULT 0;
DECLARE new_no_microplacette INT UNSIGNED DEFAULT 0;
IF NEW.deleted = 0 THEN
SELECT id_parcelle, no_microplacette
INTO new_id_parcelle, new_no_microplacette
FROM microplacette
WHERE id_microplacette = NEW.id_microplacette;
SELECT travaux, no_mesurag, id__placet
INTO vTypeTravaux, vNoMesurag, vIdPlacet
FROM secteur
LEFT JOIN parcelle ON secteur.id_secteur = parcelle.id_secteur
WHERE id_parcelle = new_id_parcelle;
IF vTypeTravaux = 'inventaire' THEN
SELECT abbreviation INTO vEssence FROM essences WHERE _id = NEW.id_essence;
IF NEW.hauteur_15 = 1 THEN
SET vClasseHau = '15CM+';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF NEW.diametre > 0 THEN
SET vClasseHau = 'D2_D8';
ELSE
SET vClasseHau = '-';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
END IF;
END IF;
END
I tried creating the procedure you show, but I don't get any error.
The error about "empty statement" happens when you try to execute a query through the API but the query string is empty.
I can duplicate the error in the mysql client this way:
mysql> set #s = '';
mysql> prepare stmt from #s;
ERROR 1065 (42000): Query was empty
So I suggest you look not at the stored procedure, but whatever code you're executing this from, and check that every time you try to execute a query, that you submit a non-empty string.
It turns out, the trigger I was updating got deleted in the meantime, so I was updating a trigger that didn't exist anymore.
I found out after refreshing the page (the trigger was gone from the trigger list).
I simply recreated the trigger anew and it worked.

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
$$

Mysql : Error in trigger

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

mysql stored procedure syntax error

Ok, this is only the second stored procedure I've written. I think you'll get the idea, I'm trying to close a credit line, and all invoices, charges, notes, etc with it. But I get a syntax error.
The goal is to CALL close_account_proc(398985994)
DELIMITER $$
CREATE
PROCEDURE `cc`.`close_account_proc`(cid INT)
#uid_usr := uid_usr FROM credit_acc WHERE type_acc = 'init' AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
INSERT INTO notes_not SET uid_usr = #uid_usr, initials_not = 'SYS',status_not = 'complete', date_not = NOW(), text_not = 'Closed credit line '.cid;
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
BEGIN
END$$
DELIMITER ;
So, anway, I get this error:
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 '#uid_usr := uid_usr from credit_acc where type_acc = 'init' and credit_used_acc ' at line 5.
Any ideas?
DELIMITER $$
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
/* Check that it's what you wanted */
SELECT uid_usr
INTO #uid_usr
FROM credit_acc
WHERE type_acc = 'init'
AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
/* Check that it's what you wanted */
INSERT
INTO notes_not (uid_usr, initials_not, status_not, date_not, text_not)
VALUES (#uid_usr, 'SYS', 'complete', NOW(), CONCAT('Closed credit line ', cid));
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
END
$$
DELIMITER ;
Hmm, im no expert at stored procedures, but isn't it.
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
// your stuff
END$$