I'm trying to create a trigger that will update some value, and if the row is not exists than insert a new row with value = 1.
I wrote the trigger in 3 possible ways and all of them are getting error
version 1:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics
SET item_id = NEW.item_id,
likes = 0,
comment = 1
ON DUPLICATE KEY UPDATE
SET comment = OLD.comment + 1;
END;
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 'SET comment = OLD.comment + 1' at line 9
version 2:
CREATE TRIGGER stat_trg AFTER INSERT ON comments
FOR EACH ROW
BEGIN
DECLARE num integer DEFAULT 0;
SELECT comment into num FROM statistics
WHERE item_id = OLD.item_id;
SET num := num + 1;
INSERT INTO statistics (item_id, comment, likes) VALUES (num,1,0)
END;
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 4
version 3:
CREATE TRIGGER stat_trg BEFORE INSERT ON comments
FOR EACH ROW
BEGIN
INSERT INTO statistics (item_id, likes, COMMENT) VALUES (NEW.item_id,0,1)
ON DUPLICATE KEY UPDATE
comment = comment + 1;
END;
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
All looks good according to examples that I found, what am I missing?
Please help on this issue.
Thanks.
Please try this:
DELIMITER $$
CREATE TRIGGER `stat_trg` BEFORE INSERT ON `comments`
FOR EACH ROW BEGIN
INSERT INTO `statistics` set item_id= NEW.item_id, likes = '0', comments = '1' ON DUPLICATE KEY UPDATE
comments = values(comments) + 1;
END$$
DELIMITER ;
Related
I cannot seem to get this trigger to work. I keep getting this error.
#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
SELECT * FROM website_queries;
CREATE DEFINER=`name`
TRIGGER `add_subscribed_users`
AFTER INSERT ON `website_queries` FOR EACH ROW
BEGIN
IF NEW.subscribed = 0 THEN
INSERT INTO users SET id=DEFAULT, firstName = NEW.firstName, lastName = NEW.lastName, mobile=DEFAULT, email = NEW.email, admin=DEFAULT, createdAt = NEW.createdAt, subscribed = NEW.subscribed;
END IF;
END; //
I am using 10.5.15-MariaDB-cll-lve doing this through phpMyAdmin
You do not need in IF.
CREATE DEFINER=`name` TRIGGER `add_subscribed_users`
AFTER INSERT ON `website_queries`
FOR EACH ROW
INSERT INTO users (firstName, lastName, email, createdAt, subscribed)
SELECT NEW.firstName, NEW.lastName, NEW.email, NEW.createdAt, NEW.subscribed
WHERE NEW.subscribed = 0;
In this single-statement form you do not need in BEGIN-END and DELIMITER too.
I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;
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.
I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error I get:
#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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?
I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)