error in trigger (mysql) - mysql

I am trying to create a trigger with an IF condition and I am getting an error on line 6.
DELIMITER $$
CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW BEGIN
IF new.Flag = 0 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID =
(SELECT SoftwareNameFK FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSE IF new.Flag = 1 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;
Error:
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 'FROM SoftwareLicenseDetails WHERE
ComputerFK=new.ComputerFK ORDER BY ID Desc LI' at line 6 0.000 sec
What am I missing?

Check this:
DELIMITER $$
CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW
BEGIN
IF new.Flag = 0 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID =
(SELECT SoftwareNameFK FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSEIF new.Flag = 1 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;

Related

Error on creating a stored procedure in MySQL, Error#1064

I'm having a hard time creating a stored procedure.
I have tried altering some things according to other entries on stack overflow but it did not help.
select version(); returns 5.7.31-0ubuntu0.16.04.1 incase that helps.
Here's the query:
DELIMITER $$
CREATE
PROCEDURE forceSubscribeToNode(IN node_id_var INT)
BEGIN
INSERT
INTO
xf_forum_watch(
user_id,
node_id,
notify_on,
send_alert,
send_email
)
SELECT
user_id,
node_id_var AS node_id,
"thread" AS notify_on,
1 AS send_alert,
1 AS send_email
FROM
xf_user
WHERE
user_group_id NOT IN(
1, 18, 40
)
ON DUPLICATE KEY
UPDATE
send_alert = 1,
send_email = 1,
notify_on = CASE WHEN notify_on IS NULL OR notify_on = '' THEN 'thread' ELSE notify_on END
END $$
DELIMITER ;
Here's the 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 'END' at line 30
(Line 18 is "send_email = 1,"
For context, search engines and future reference:
This will be used for the forum software Xenforo 2.1 to force subscribe users to certain nodes like categories and sub-forums.
So it seems I forgot to add a ; with a space before after the first END
DELIMITER $$
CREATE
PROCEDURE forceSubscribeToNode(IN node_id_var INT)
BEGIN
INSERT
INTO
xf_forum_watch(
user_id,
node_id,
notify_on,
send_alert,
send_email
)
SELECT
user_id,
node_id_var AS node_id,
"thread" AS notify_on,
1 AS send_alert,
1 AS send_email
FROM
xf_user
WHERE
user_group_id NOT IN(
1, 18, 40
)
ON DUPLICATE KEY
UPDATE
send_alert = 1,
send_email = 1,
notify_on = CASE WHEN notify_on IS NULL OR notify_on = '' THEN 'thread' ELSE notify_on END ;
END $$
DELIMITER ;

Django migration sql for conditional triggers

I want to create a trigger that only inserts into table when the condition is met.
I've tried using various combinations of IF/BEGIN/END and WHERE, but Django returns me an SQL syntax error each time.
Here, type_user_id refers to the person who triggers the like, and user_id refers to the person who receives the notification. It should only trigger when they are not the same person.
operations = [
migrations.RunSQL(
"DROP TRIGGER like_notification",
"""
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
IF (select user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)) <> NEW.user_id
BEGIN
INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
(SELECT user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
)
END;
"""
)
]
>>>django.db.utils.ProgrammingError: (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\n INSERT INTO notification (type, type_id, article_id, is_re' at line 5")
and
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
BEGIN
DECLARE #user_same Boolean;
SELECT 1 into #user_same
FROM channel
WHERE channel_id IN (select channel_id FROM article where article_id = NEW.article_id)
AND user_id = NEW.user_id;
IF #user_same <> 1
THEN
INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id,)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id, (
SELECT user_id from channel WHERE channel_id IN (
SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
END IF;
END;
>>>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 '#user_same Boolean;\n SELECT 1 into #user_same\n FRO' at line 4"
and
CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
FOR EACH ROW
INSERT INTO notification (type, type_id, article_id, is_read, type_user_iduser_id)
VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
(
SELECT user_id from channel WHERE channel_id IN
(SELECT channel_id FROM article WHERE article_id = NEW.article_id)
)
WHERE NOT user_id = NEW.user_id;
>>>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 'WHERE NOT user
I don't really understand why none of them work. Any help would be much appreciated!
#variables are not DECLARED.
Either:
DECLARE user_same BOOLEAN;
SELECT 1 INTO user_same WHERE ...
or
SELECT #user_same := 1 WHERE ...
Better yet, this avoids the need for the variable.
IF (EXISTS SELECT * FROM ...)
Also, don't use the construct IN ( SELECT ... ); it is usually better to use WHERE EXISTS ( SELECT * FROM ... ) or JOIN ... ON ....
(There may be more problems after fixing those.)
Another easy way to do this.
You should add any symbols between ; delimiters for the appropriate converting MySQL syntax to the raw SQL.
CREATE TRIGGER trigger_name BEFORE INSERT ON table
FOR EACH ROW
BEGIN
IF NEW.number <> 'anynumber' AND NEW.number <> 'anynumber'
THEN
SET NEW.number = 'anynumber'; --
END IF; --
END
This example works due to the dash symbols.

ERROR 1064 (42000): check the manual that corresponds to your MySQL server version for the right syntax to use near 'END'

here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
ERROR 1064 (42000): 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' at line 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;

MySQL Stored Procedure Issue #1064

This little one gives me syntax problem, i cant solve that i'll appreciate that.
DELIMITER $$
CREATE procedure kullanicibilgicek
(
p_kadi varchar(50)
)
begin
declare v_sonuc INT;
if(CHAR_LENGTH(RTRIM(p_kadi)) > 4)
then
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi)
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi;
else
set v_sonuc = 0;
select v_sonuc;
end if;
else
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where id = Cast(p_kadi as int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where id = cast(p_kadi as int);
else
set v_sonuc = 0;
select v_sonuc;
end if;
end if;
END;
$$
DELIMITER ;
#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 'int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1' at line 18

Getting error on limit in MySQL

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 'limit 0, 30'
I get this error when I try to use this code :
DROP TABLE IF EXISTS prodaja;
CREATE TABLE prodaja ( id SERIAL, id_stvari BIGINT, id_kupca BIGINT, kvantitet INT, cijena DECIMAL(9,2), podmireno INT );
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (1, 3, 5, 19.95, 0);
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (2, 2, 3, 14.95, 1);
INSERT INTO prodaja (id_stvari, id_kupca, kvantitet, cijena, podmireno) VALUES (3, 1, 1, 29.95, 0);
SELECT * FROM prodaja;
DELIMITER //
CREATE TRIGGER updateprodaja BEFORE UPDATE ON prodaja
FOR EACH ROW
BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: Ne mogu azurirati red imena podmireno u tablici prodaja';
END IF;
END
//
DELIMITER ;
START TRANSACTION;
UPDATE prodaja SET kvantitet = kvantitet + 9 WHERE id = 1;
COMMIT;
I really don't know where the problem is... Can someone help me out?
You are using a UI that automatically tacks on LIMIT 30 to whatever you enter? What version of what UI? Also, what version of MySQL?
Solution: Upgrade the UI.