syntax error in create trigger, what's wrong? - mysql

can somebody tell me what is wrong with the syntax of my code please? (this is the exact and only code I'm highlighting and running so line numbers should match up)
CREATE TRIGGER `trg_part_upd` AFTER UPDATE ON `tbl_sub_model_eng_trans_part`
FOR EACH ROW
if NEW.engine_sk = 0 and NEW.trans_sk = 0
then
UPDATE tbl_sub_model tsm
INNER JOIN tbl_fct_sub_eng_trans tfset ON tsm.sub_model_sk = tfset.sub_model_sk
INNER JOIN tbl_sub_model_eng_trans_part tsmetp ON tfset.trans_sk = tsmetp.trans_sk
SET tsm.last_modified_date = NOW()
WHERE tsmetp.sub_model_sk=NEW.sub_model_sk;
end if;
I get these two errors:
Error Code: 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 9
Error Code: 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 'end if' at line 1

You forgot to change the delimiter, so MySQL thinks your first statement is this:
CREATE TRIGGER `trg_part_upd` AFTER UPDATE ON `tbl_sub_model_eng_trans_part`
FOR EACH ROW
if NEW.engine_sk = 0 and NEW.trans_sk = 0
then
UPDATE tbl_sub_model tsm
INNER JOIN tbl_fct_sub_eng_trans tfset ON tsm.sub_model_sk = tfset.sub_model_sk
INNER JOIN tbl_sub_model_eng_trans_part tsmetp ON tfset.trans_sk = tsmetp.trans_sk
SET tsm.last_modified_date = NOW()
WHERE tsmetp.sub_model_sk=NEW.sub_model_sk;
Just add this before the code:
DELIMITER $$
... and this afterwards:
$$
... so MySQL can recognize the complete trigger as a single statement.
You can change $$ for your own choice.
Official docs give details on this in the Defining Stored Programs section.
Please note that this issue only affects MySQL clients that can accept more than one statement at once so they implement a separator to tell them apart. DELIMITER is a command of the mysql command-line tool; it may or may not be implemented in other clients.

Try This:
DELIMITER $$
CREATE TRIGGER `trg_part_upd` AFTER UPDATE ON `tbl_sub_model_eng_trans_part`
FOR EACH ROW BEGIN
IF NEW.engine_sk = 0 AND NEW.trans_sk = 0
THEN
UPDATE tbl_sub_model tsm
INNER JOIN tbl_fct_sub_eng_trans tfset ON tsm.sub_model_sk = tfset.sub_model_sk
INNER JOIN tbl_sub_model_eng_trans_part tsmetp ON tfset.trans_sk = tsmetp.trans_sk
SET tsm.last_modified_date = NOW()
WHERE tsmetp.sub_model_sk=NEW.sub_model_sk;
END IF;
END;
$$
DELIMITER ;

The trigger is a source object, it has a body - one or more internal statements.
If there are some statements in body, then body must be wrapped with BEGIN...END clause. In this case you may also need to use client DELIMITER command for the CREATE TRIGGER.
If you had one statement in the body, then you could use syntax without BEGIN...END, and without DELIMITER command.

Related

Cannot run mysql syntax on select

I tried running this in phpmyadmin...
It turned out something is not right.
I cannot figure out what is wrong here.
DELIMITER ;
create definer=proiect_wd_user#localhost FUNCTION
f_suma(id_s int, price_f double,product_code_n char(255),quantity_b int)
returns double
BEGIN
select price_f into #price_f
from orders_details WHERE (id=id_s)
select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
end ;
Error:
MySQL said: Documentation
#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 'select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #su' at line 7
There are various problems here with your delimiters. See Working with Stored Procedures in the MySQL documentation.
DROP FUNCTION IF EXISTS f_suma;
DELIMITER //
CREATE DEFINER=proiect_wd_user#localhost
FUNCTION f_suma(id_s int,price_f double,product_code_n char(255),quantity_b int)
RETURNS double
BEGIN
SELECT price_f INTO #price_f FROM orders_details WHERE (id=id_s);
SELECT quantity_b INTO #quantity_b FROM orders_details WHERE (id=id_s);
SET #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
END //
DELIMITER ;

Unidentifiable error in SQL syntax, thrown when trying to store a procedure

I am getting an error when I try to enter the following procedure using the phpMyAdmin SQL box. I'm pretty sure the syntax is correct though!
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
')
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = de' at line 1
SQL SYNTAX:
delimiter $$
CREATE PROCEDURE DeleteCard(IN aCard varChar)
BEGIN
UPDATE decks
JOIN amount ON amount.DeckName = decks.DeckName
SET decks.DeckTotal = decks.DeckTotal - amount.Amount
WHERE amount.CardName = aCard ;
UPDATE types t1
JOIN cards ON cards.TypeName = t1.TypeName
JOIN Amount ON amount.CardName = cards.CardName
SET t1.TypeTotal = t1.TypeTotal - amount.Amount
WHERE amount.CardName = aCard ;
DELETE
FROM amount
WHERE cardName = aCard;
DELETE
FROM cards
WHERE cardName = aCard;
END
$$
DELIMITER ;

MySQL Trigger not saving

Executing this:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END
Results in this:
/* SQL 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 8 */
What am I missing? This should work, shouldn't it?
Thanks
You're most likely trying to add the trigger without changing the delimiter. Since the trigger contains semicolon, you'll have to change the delimiter to something else temporarily;
DELIMITER //
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
BEGIN
UPDATE hb_accounts
SET hb_accounts.domain = (SELECT companyname FROM hb_client_details
WHERE hb_client_details.id = NEW.client_id
LIMIT 1)
WHERE hb_accounts.client_id = NEW.client_id;
END //
DELIMITER ;
An SQLfiddle with the trigger adding successfully. Note that the delimiter is changed in the settings,
Here's another implementation:
CREATE TRIGGER `after_order_insert`
AFTER INSERT ON `hb_orders` FOR EACH ROW
UPDATE hb_accounts a
join hb_client_details b on a.client_id = b.id and b.id = new.client_id
set a.domain = b.companyname;

MySQL Syntax error near ''

I'm a lot more used to T-SQL than MySQL and it seems as though there are slight syntax issues that I just can't quite figure out. I'm getting an error message that seems quite meaningless to me and I would really appreciate it if someone could just tell me what I'm doing wrong here. Am I perhaps not allowed to do an UPDATE in an UPDATE TRIGGER?
The idea is that I want to just keep track of whether or not my current German log has been corrected or not and record the times based on whether or not I'm updating my portion of the log or my tutor is updating their portion.
My Code is:
DELIMITER //
CREATE TRIGGER updateTimes
AFTER UPDATE ON Logs
FOR EACH ROW
BEGIN
IF (Old.TimGerman <> New.TimGerman OR
Old.TimComment <> New.TimComment)
THEN
UPDATE Logs
SET DateUpdated = CURRENT_TIMESTAMP, Corrected = 0
WHERE LogID = New.LogID;
ELSE IF (Old.TutorGerman <> New.TutorGerman OR
Old.TutorComment <> New.TutorComment)
THEN
UPDATE Logs
SET DateMarked = CURRENT_TIMESTAMP, Corrected = 1
WHERE LogID = New.LogID;
END IF;
END //
DELIMITER ;
Me error message says:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax near '' at line 21.
Line 21 is 4th from the bottom: WHERE LogID = New.LogID;
Thanks very much for the help!
Syntactically your trigger is correct except on ELSE IF part. You need an extra END IF if you want to use it. Otherwise modify it as ELSEIF.
And also it seems you need a BEFORE trigger but not AFTER trigger.
Apart from that, calling the explicit update in an update trigger on the same table is meaning less as it would cause a circular event. Which is not supported and throws an error.
Change your trigger definition as below:
DELIMITER //
CREATE TRIGGER updateTimes BEFORE UPDATE ON Logs
FOR EACH ROW
BEGIN
IF ( OLD.LogID = New.LogID ) THEN
IF ( Old.TimGerman <> New.TimGerman OR
Old.TimComment <> New.TimComment )
THEN
SET NEW.DateUpdated = CURRENT_TIMESTAMP, NEW.Corrected = 0;
ELSEIF ( Old.TutorGerman <> New.TutorGerman OR
Old.TutorComment <> New.TutorComment )
THEN
SET NEW.DateMarked = CURRENT_TIMESTAMP, NEW.Corrected = 1;
END IF;
END IF;
END;
//
DELIMITER ;

mysql trigger using custom function

I am trying to write a MySQL trigger that, upon update of a row, will call a custom function (which works fine outside of the trigger) and update a column in the table the trigger is on.
BEGIN
UPDATE candles
SET can_materials_cost = (SELECT calculateMaterialCost(NEW.can_id)
WHERE candles.can_id = NEW.can_id);
END
I've written basic audit style triggers that upon update save the old value into a different table, but for some reason when trying to use my custom function I receive an error stating:
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 'WHERE candles.can_id = NEW.can_id);
END' at line 4
The custom function performs:
BEGIN
-- This function assumes that the raw wax cost is for 20kg bags only - BEWARE!
DECLARE result DECIMAL(10,2);
SET result = (
(select calculateWaxCost(_can_id))
+
(select calculateDyeCost(_can_id))
+
(select calculateScentCost(_can_id)));
RETURN result;
END