MySQL trigger: undefined variable 'tableName' - mysql

I am unable to see what is wrong with this trigger definition...I don't know if the select query is faulty or somewhere else.
The idea is when a row is inserted in tableA, the trigger performs a SELECT on tableB to retrieve some data and insert or update tableC.
Error:
SQL Error [1327] [42000]: Undeclared variable: aTable
Trigger DDL:
DELIMITER //
CREATE TRIGGER my_trigger
AFTER INSERT
ON myTable
FOR EACH ROW
BEGIN
DECLARE varA integer;
DECLARE varB integer;
IF NEW.`Type` = 'comment' THEN
SELECT
myOtherTable.Id INTO varA
,aTable.Id INTO varB
FROM myOtherTable <----- maybe here
JOIN aTable ON aTable.id = myOtherTable.aTableId
WHERE myOtherTable.Id = NEW.Id;
Insert INTO myThirdTable VALUES
(NEW.Id, NEW.time, varA)
ON DUPLICATE KEY UPDATE ...;
END IF;
END//
DELIMITER ;
UPDATE 03/12/2020
It seems that adding a second INTO varB leads to the error that the aTable variable is undeclared.

Test single-statement form:
CREATE TRIGGER my_trigger
AFTER INSERT
ON myTable
FOR EACH ROW
INSERT INTO myThirdTable (id, `time`, a)
SELECT NEW.Id, NEW.time, myOtherTable.Id
FROM myOtherTable
WHERE myOtherTable.Id = NEW.Id
ON DUPLICATE KEY UPDATE ...;

Related

WHATS WRONG IN FOLLOWING CODE,

CREATE OR REPLACE TRIGGER T12
AFTER INSERT OR UPDATE OR DELETE
ON EMP
FOR EACH ROW
DECLARE
OP VARCHAR2(20);
NEW NUMBER(5);
BEGIN
IF INSERTING THEN
OP := 'INSERT';
ELSIF UPDATING THEN
OP :='UPDATE';
ELSE
OP :='DELETE';
END IF;
INSERT INTO EMP_AUDIT
VALUES(:NEW.EMPNO,:NEW.ENAME,:NEW.SAL,:OLD.EMPNO,:OLD.ENAME,:OLD.SAL);
END;
You have an error in your SQL syntax; it seems the error is around: 'TRIGGER T12 AFTER INSERT OR UPDATE OR DELETE ON EMP FOR EACH ROW DECLARE O'
CREATE
OR REPLACE TRIGGER T12
AFTER
INSERT
OR
UPDATE
OR DELETE ON EMP FOR EACH ROW DECLARE OP VARCHAR2(20);NEW
NUMBER(5);BEGIN IF INSERTING THEN OP: = 'INSERT';ELSIF UPDATING THEN
OP:
= 'UPDATE';ELSE OP: = 'DELETE';END IF;
INSERT INTO
EMP_AUDIT
VALUES(
:NEW.EMPNO,
:NEW.ENAME,
:NEW.SAL,
:OLD.EMPNO,
:OLD.ENAME,
:OLD.SAL
);END;
Please check the TRIGGER T12

After Insert MySQL Trigger to update values or insert a new row in another table

I have two tables,
RawFeed , SellerList.
When a new row is inserted in RawFeed, I need to insert a row in SellerList if a corresponding SellerID does not exist in SellerList,
Otherwise, I need to increment two column values in the SellerList table for that SellerID.
Here is my Trigger code-
CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
BEGIN
--CHECK SELLER ID EXISTS OR NOT.
DECLARE SELLEREXISTS INT;
SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;
IF SELLEREXISTS=0 THEN
--INSERT ROW
INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
ELSE
--UPDATE ROW
UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
END IF;
END
And I am receiving this error-
SQL Error(1064): You have an error in your SQL Syntax;
Try changing the delimiter
DELIMITER $$
CREATE TRIGGER `SellerListUpdate` AFTER INSERT ON `RawFeed`
FOR EACH ROW
BEGIN
--CHECK SELLER ID EXISTS OR NOT.
DECLARE SELLEREXISTS INT;
SELECT COUNT(*) INTO SELLEREXISTS FROM SellerList WHERE SellerList.SellerID=NEW.seller_id;
IF SELLEREXISTS=0 THEN
--INSERT ROW
INSERT INTO SellerList(`SellerID`, `Total`, `Active`) VALUES(NEW.seller_id, 1, 1);
ELSE
--UPDATE ROW
UPDATE SellerList SET Total=Total+1, Active=Active+1 WHERE SellerList.SellerID=NEW.seller_id;
END IF;
END
$$
DELIMITER ;

Error #1109 in MySql

This is my trigger.
I want to make trigger on 1 table (rezervare).
Trigger
DELIMITER //
CREATE TRIGGER verificare_nr_locuri
BEFORE INSERT ON Rezervare
FOR EACH ROW
BEGIN
IF ( SELECT Masa.NrLocuri FROM Rezervare, Masa
WHERE Masa.NumarMasa = Rezervare.NumarMasa ) < Rezervare.NumarPersoane THEN
SET NEW.NumarMasa = NULL;
END IF;
END //
DELIMITER ;
INSERT INTO Rezervare VALUES (123,106,2,'2016-12-11','2016-12-24',5);
INSERT INTO Rezervare VALUES (124,106,2,'2016-12-11','2016-12-24',4);
When I execute the trigger, it has been created. But, when I insert data into table rezervare, It become
#1109 - Unknown table 'rezervare' in field list.
How can I resolve this?
Table
I think you might need to use a JOIN for your SELECT clause.
BEGIN
IF (SELECT Masa.NrLocuri, Rezervare.NumarMasa FROM Masa JOIN Rezervare
WHERE Masa.NumarMasa = Rezervare.NumarMasa) < Rezervare.NumarPersoane THEN
SET NEW.NumarMasa = NULL;
END IF;

INSERT INTO if conditions are met

A user is only meant to have up to 3 keys registered to his account at any one time. To add a new key, the user must first delete another key to "make room" for a new one.
I want this to be checked server-side, but I can't get the query to work. Here is what I tried:
IF (SELECT COUNT(serial_key_nbr)
FROM keys_table WHERE user_id = 9) <= 2
THEN INSERT INTO keys_table (user_id, serial_key_nbr)
VALUES (9, 'abc123')
How to do this?
You can use the below mention Script for the same:
INSERT INTO keys_table (user_id, serial_key_nbr)
SELECT 9, 'abc123' FROM DUAL
WHERE
(SELECT COUNT(serial_key_nbr)
FROM keys_table WHERE user_id = 9)<=2
if you want to use an if to do a conditional select then I would put it in a variable like so.
BEGIN
DECLARE var1 INT;
SELECT COUNT(serial_key_nbr) INTO var1
FROM keys_table
WHERE user_id = 9;
IF var1 <= 2
THEN
INSERT INTO keys_table (user_id, serial_key_nbr)
VALUES (9, 'abc123')
END IF;
A trigger might be the way to go. If a condition is met, a trigger before inserting in the table can perform an invalid operation and cause the insert operation to fail:
delimiter $$
create trigger keep_three before insert on keys_table for each row
begin
if (select count(serial_key_nbr) from keys_table where user_id = new.user_id) >= 3 then
insert into non_existent_table (non_existent_field) values (new.user_id);
end if;
end$$
delimiter ;
Ugly, but it might work.
Reference:
"MySQL Triggers: How do you abort an INSERT, UPDATE or DELETE with a trigger?"
Another solution (better I think) is to forcibly delete an entry before attepting the insert. When there are less than 3 entries, the insert procedes normally:
delimiter $$
create trigger keep_three before insert on keys_table for each row
begin
while (select count(serial_key_nbr) from keys_table where user_id = new.user_id) >= 3 do
delete from keys_table where user_id = new.user_id
-- OPTIONAL: Add an ordering criteria to define which entry is deleted first
limit 1;
end while;
end$$
delimiter ;
I think this is cleaner.
A third way (I've found it here). It will return an error message (by signaling sqlstate 45000: Unhandled user defined exception) associated with the defined condition:
delimiter $$
create trigger keep_three before insert on keys table for each row
begin
declare msg varchar(255);
declare n int default 0;
set n = (select count(serial_key_nbr) from keys_table where user_id = new.user_id);
if n >= 3 then
set msg = "INSERT failed: There must be only three entries for each user. Delete an entry first";
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
end if;
end$$
delimiter ;
A cleaner version of my first option.

how to get value from select query in trigger in mysql5?

How to get value from select query in trigger and insert that value in table?
For an INSERT Trigger query you would use the object NEW
For an UPDATE Trigger query you would use the object OLD and NEW
For a DELETE Trigger query you would use the object OLD
Example 1 : iF you ran INSERT INTO mytable (num) VALUES (10);
In the INSERT trigger, you reference the column as NEW.num (10);
Example 2 : iF you ran UPDATE mytable SET num = 41 WHERE num = 10;
In the UPDATE trigger, you reference OLD.num (10) and NEW.num (41)
Example 3 : iF you ran DELETE mytable num = 104;
In the DELETE trigger, you reference OLD.num (104)
Use something like this:
DELIMITER $$
create trigger my_trigger
AFTER UPDATE on my_update_table
for each row
begin
DECLARE P1,P2 VARCHAR(50);
SELECT PRICENAME INTO P1 FROM PRICEIES WHERE PRICEID=OLD.PRICEID;
SELECT PRICENAME INTO P2 FROM PRICEIES WHERE PRICEID=NEW.PRICEID;
INSERT INTO AUDITLOG(OLDVALUE, NEWVALUE) VALUES (P1,P2);
end $$
DELIMITER ;