MySQL after update trigger - hidden syntax error - mysql

the following trigger cannot be created on my local testing db (MySQL 5.6) and I do not understand why.
DELIMITER //
CREATE TRIGGER write_status_history AFTER UPDATE ON request
FOR EACH ROW
BEGIN
DECLARE snew CHAR;
DECLARE sold CHAR;
DECLARE unew CHAR;
DECLARE uold CHAR;
-- Status change
IF NEW.status <> OLD.status THEN
SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
SET snew = (SELECT name FROM status WHERE id = NEW.status);
SET sold = (SELECT name FROM status WHERE id = OLD.status);
INSERT INTO history (`request_id`, `content`)
VALUES(NEW.id, CONCAT('Statuswechsel von ', sold, ' nach ', snew, ' durchgeführt von ', user, '.'));
-- User change
ELSE IF NEW.ta_users_id <> OLD.ta_users_id THEN
SET unew = (SELECT username FROM ta_users WHERE id = NEW.ta_users_id);
SET uold = (SELECT username FROM ta_users WHERE id = OLD.ta_users_id);
INSERT INTO history (`request_id`, `content`)
VALUES(NEW.id, CONCAT('Bearbeiter gewechselt von ', uold, ' nach ', unew, '.'));
END IF;
END
//
DELIMITER ;
I get this error messsage indicating a syntax error if I attempt to launch the query:
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 21.
MySQL Workbench, however, reports Syntax Error: missing 'if' when hovering over the last END.
Do you have an idea how to fix this syntax error? I already tried adding a semicolon to the last END, that didn't help - same error.

Mysql else-if would be as
elseif
When you give a space it will consider else and then another if and will expect the inner if to be closed.

Related

Mysql trigger to insert the row if not exist in table but doesn't work

I am trying to create a trigger to check if a row with the same subject and object names is created. If not, I want to insert it else I will show an error message.
I have looked up some examples and written the code below but there are some errors.
DELIMITER $$
CREATE TRIGGER insertOnceKB BEFORE INSERT ON knowledgebase FOR EACH ROW
BEGIN
IF NEW.Object, NEW.Subject, NEW.Predicate not in
(SELECT A.Object, A.Subject, A.Predicate FROM knowledgebase as A
where (NEW.Object = A.Object and NEW.Subject = A.Subject and NEW.Predicate = A.Predicate)) THEN
INSERT INTO knowledgebase (Object,Predicate,Subject,Count,botID,Source,EntityTypeID)
VALUES ( NEW.Object, NEW.Predicate, NEW.Subject, NEW.Count, NEW.botID, NEW.Source, NEW.EntityTypeID);
THEN
CALL `Insert not allowed`;
END IF;
END $$
DELIMITER ;
This is the error message that Xampp server showed:
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 ' NEW.Subject, NEW.Predicate not in (SELECT A.Object, A.Subject,
A.Predicate FR' at line 3
this statement
IF NEW.Object, NEW.Subject, NEW.Predicate not in (SELECT A.Object, A.Subject, A.Predicate FROM knowledgebase as A where (NEW.Object = A.Object and NEW.Subject = A.Subject and NEW.Predicate = A.Predicate))
is wrong, even if it works out, I don't think a trigger would allow to query the very table you're about to insert in, but anyway,
you can do it this way
if exists( select * from knowledgebase as a where new.Subject = a.Subject and new.Predicate = a.Predicate and new.Object = a.Object ) then
return bla bla bla;
else if
do whatever you were doing...
end if

MySQL Stored Procedure Case Statement Syntax Error - Contd 2

One Syntax Error in the following code.
The response of the mysql was
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 ''300ml','500ml','1lit','2lit')
VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Book' at line 24 SQL
Statement
The following is the procedure.
CREATE PROCEDURE `calculate_amount` (in IN_book_id INT, in IN_qty INT )
BEGIN
-- declare
DECLARE m_prdct VARCHAR(10);
DECLARE m_cust_id INT(5);
-- select into
SELECT
Product
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_prdct;
SELECT
Cust_id
FROM
Bookings
WHERE
Book_id = IN_book_id INTO m_cust_id;
-- conditionals & action
IF (m_prdct = '20ltr') THEN
INSERT INTO Amount (Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,(SELECT Rate.Can*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0,0);
ELSEIF (m_prdct = '300ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,(SELECT Rate.300ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0,0);
ELSEIF (m_prdct = '500ml') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,(SELECT Rate.500ml*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0,0);
ELSEIF (m_prdct = '1ltr') THEN
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,(SELECT Rate.1lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id),0);
ELSE
INSERT INTO Amount(Cust_id,Book_id,Can,'300ml','500ml','1lit','2lit') VALUES(m_cust_id,IN_book_id,0,0,0,0,(SELECT Rate.2lit*Bookings.Qty FROM Rate,Bookings WHERE Bookings.Book_id=IN_book_id));
-- end
END IF;
END
The script applied successfully even if it showed error. The Back Tick to column names starting with digits solved the issue. Upon Refreshing, it got changed from _SYNTAX_ERROR to its actual name.

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

Sql trigger's query

I am trying to create custom id for my table in the following format
2 random alphabets - 00MaxID
for e.g: AA-001
I have tried writing a query for it but it is not working, this is my first time writing a trigger also writing a complex query such as this.
UPDATED-2
the following query gives me an error near "SELECT count(cus_id) INTO #ct FROM customer;"
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO #ct FROM customer;
IF #ct < 1000 THEN
SET #cs_id = LPAD(#ct+1, 3, 0 );
ELSE
SET #cs_id = #ct+1;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',#cs_id);
END;
Error
SQL query: Documentation
CREATE
TRIGGER `id_gen` BEFORE INSERT
ON `testdb`.`customer`
FOR EACH ROW BEGIN
SELECT count(cus_id) INTO #ct FROM customer
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 '' at line 6
Executing with Trigger Section
Answered by #Solarflare.
In the screenshot, you can see that phpmyadmin automatically added code including for each row, which is now twice in the statement (thus the error). Your own code will start with begin.
BEGIN
SELECT count(cus_id)+1 INTO #ct FROM customer;
IF #ct < 1000 THEN
SET #cs_id = LPAD(#ct, 3, 0 );
ELSE
SET #cs_id = #ct;
END IF;
SET NEW.cus_id = CONCAT(CHAR(FLOOR(65 + RAND() * 26),FLOOR(65 + RAND() * 26)),'-',#cs_id);
END;

Keep Getting a Syntax Error when Creating MySQL Trigger

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.