Keep Getting a Syntax Error when Creating MySQL Trigger - mysql

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
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 6

The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

Related

MySQL Create Trigger Syntax Error (Last Line)

I'm creating a MySQL trigger designed to update various tables with a new value if certain values are changed by an UPDATE query. I keep receiving the following syntax error for this particular trigger:
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 29
Line 29 in this case is the line of the END statement.
This is my full code:
DELIMITER $$
CREATE TRIGGER update_selling_prices BEFORE UPDATE ON t1
FOR EACH ROW
BEGIN
DECLARE update_price INT DEFAULT 0;
DECLARE selling_price_1 DECIMAL(10, 3) DEFAULT 0.000;
DECLARE selling_price_2 DECIMAL(10, 3) DEFAULT 0.000;
IF (OLD.rrp_price <> NEW.rrp_price OR OLD.discount_1 <> NEW.discount_1 OR OLD.discount_2 <> NEW.discount_2 OR OLD.net_price <> NEW.net_price OR OLD.markup <> NEW.markup OR OLD.delivery_cost <> NEW.delivery_cost) THEN
SET update_price = (SELECT b.is_auto_update FROM price_categories c INNER JOIN brands b ON b.brand_name = c.brand_name WHERE c.id = NEW.category_id LIMIT 1);
IF (update_price = 1) THEN
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
END IF;
END IF;
END;
$$
DELIMITER ;
The current UPDATE statements are just placeholders for some actual calculations I'll end up doing.
Please note: I use Sequel Pro for most MySQL-related stuff and initially was using their GUI to try and add the trigger - it automatically adds the surrounding code so I would only create everything between the BEGIN and END statements. That also resulted in this same syntax error, so I don't believe it's related to the delimiters like some similar threads I've already found on here. Nevertheless, I've tried adding the full trigger code via a normal query; changing the delimiter syntax - for example END$$, END $$, END; $$ etc.
I've successfully created other triggers with similar syntax, but they do not include the DECLARE syntax.
Where am I going wrong?
The problem is here:
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
Review documentation: https://dev.mysql.com/doc/refman/8.0/en/if.html
MySQL supports ELSEIF and this is different than ELSE IF. If you use ELSEIF, this continues the structure of the IF statement. If you use ELSE IF, it starts a new IF statement, so it should be like this:
IF (NEW.is_bundle = 0) THEN
UPDATE t2 SET temp = 'Fired Single' WHERE id = NEW.id;
ELSE
IF (NEW.is_bundle = 1) THEN
UPDATE t2 SET temp = 'Fired Bundle' WHERE id = NEW.id;
END IF;
END IF;
See that there is a complete IF/THEN/END IF statement within the ELSE block of the outer one?
But you didn't do that, so the END IF applies to the innermost IF statement, and then you're one level off for the rest of the body of the trigger.
When MySQL gets to the end of the whole CREATE TRIGGER statement, if there aren't enough ENDs to balance the blocks you began, MySQL complains with the error you saw.

I want to generate a varchar code with mysql

I want to generate a code with mysql something like 'B45'. the generation starts at 'A0' and ends in 'Y99'
, but somehow there is a problem, please I need help with my code
CREATE TRIGGER oeuvre_code BEFORE INSERT ON oeuvres
BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2 ='A0';
ELSE
SET #test = (select code from oeuvres LIMIT 1);
SET #char =(select left (#test,1));
SET #nbr = select substr(#test,2);
if #char!='Z' AND #nbr+0 <99 THEN
set #char2 = select (ASCII(#char)+1);
set #nbr2 = #nbr+1;</i>
END IF;
set #code2=#char2+#nbr2;
INSERT INTO oeuvres VALUES(NEW.Id,NEW.Toile,NEW.Description,NEW.Prix,NEW.Date,NEW.Etat,#code2);
END IF;
END;
Here is the problem I get:
Error
SQL query: Documentation
CREATE TRIGGER oeuvre_code BEFORE INSERT ON oeuvres
BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2 ='A0'
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 'BEGIN
IF NOT EXISTS(select * from oeuvres) THEN
SET #code2' at line 2
Picture of the problem I get

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 ;

What is wrong with this trigger?

I want to set only one row actived.
If somebody try to set the estrutura_versao_status as 1, set every other estrutura_versao_status to 0 before and keep this new row as the only one estrutura_versao_status = 1.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON `sys_estrutura`
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
UPDATE `sys_estrutura` SET estrutura_versao_status = 0;
END IF;
END;
This is the 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 '' at line 5
Assuming you want to set the flag for the newly inserted row, just use assign that to the NEW variable. There is no need to do an UPDATE on the table.
CREATE TRIGGER tgr_classifica_ativa_revisao BEFORE INSERT ON sys_estrutura
FOR EACH ROW
BEGIN
IF (NEW.estrutura_versao_status = 1) THEN
SET NEW.estrutura_versao_status = 0;
END IF;
END;