WHATS WRONG IN FOLLOWING CODE, - plsqldeveloper

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

Related

MySQL trigger: undefined variable 'tableName'

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 ...;

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;

Trigger creation query failed

I have three tables, when data is inserted into third table, I am checked whether newly inserted row has field value in table1. If value exists then I am moving the same row from table3 to table2. My code is like below, But I am getting error while executing this
DELIMITER $$
CREATE TRIGGER onRosterAdd
AFTER INSERT ON yet_tobe
FOR EACH ROW
BEGIN
SELECT CASE WHEN (SELECT 1 FROM users WHERE NEW.jid = (users.username + "_1")) > 0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END
END$$
DELIMITER ;
And here 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 'INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
How can I fix this.
Try it like this:
DELIMITER $$
CREATE TRIGGER onRosterAdd
BEFORE INSERT ON yet_tobe
FOR EACH ROW
BEGIN
IF (SELECT 1 FROM users WHERE CONCAT(username,"_1")=NEW.jid)>0
THEN
INSERT INTO rusers SELECT * FROM yet_tobe WHERE yet_tobe.id = NEW.id;
DELETE FROM yet_tobe where yet_tobe.id = NEW.id;
END IF;
END$$
DELIMITER ;
Here's a working fiddle

MYSQL Error when creating a trigger

I have the following MySQL trigger query :
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$
When I run the Query I get the following MySQL error : Error
SQL query:
CREATE TRIGGER `after_insert_stock` AFTER INSERT ON `stock_audit`
FOR EACH
ROW BEGIN
IF NEW.deleted
THEN
SET #changetype = 'DELETE';
MySQL said: Documentation
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 7
Please can some one help solving the problem?
You've missed DELIMITER declaration:
DELIMITER $$
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$
Try this:
DELIMITER $$
CREATE TRIGGER after_insert_stock AFTER INSERT ON stock FOR EACH ROW BEGIN
DECLARE changetype varchar;
IF NEW.deleted THEN
SET changetype := 'DELETE';
ELSE
SET changetype := 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, :changetype);
END$$
Try this simplified query -
CREATE TRIGGER `after_insert_stock`
AFTER INSERT
ON `stock`
FOR EACH ROW
INSERT INTO
stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype)
VALUES
(NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, IF(NEW.deleted, 'DELETE', 'NEW'));

trigger update on column change

Hello currently my trigger updates on table update, and I need to change this to only fire when specific column changes.
code:
create trigger money after update on things
for each row
begin
UPDATE `current` c
INNER JOIN things t ON c.id2 = t.id2
INNER JOIN dude_base d ON d.id1 = c.is1
SET c.`curr_cash` = t.thing_cost * d.salary / 100;
end;
$$
And I need to change so it will turn on, when there is update on "thing_cost" from things.
#edit
I have to update that curr_cash and things cost are totaly different values, I cannot compare them, they will always be different.
When I used
if NEW.things_cost <> OLD.things_cost
I get following 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 10
#edit
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id = s.id
SET s.`curr_cash` = u.thing_cost * b.salary/ 100;
end if;
$$
Try this code...
**create table sales** (orderno INT, sale INT,empsalary int, ts TIMESTAMP);
**create table history** (updated varchar(20), oldvalue INT,newvalue INT);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(1,700,7000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(2,800,8000);
INSERT INTO **sales** (orderno,sale,empsalary) VALUES(3,900,9000);
DROP TRIGGER test.instrigger;
DELIMITER ///
CREATE TRIGGER test.instrigger AFTER UPDATE ON sales
FOR EACH ROW
BEGIN
IF NEW.sale <> OLD.sale THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('sale', OLD.sale,NEW.sale);
END IF;
IF NEW.empsalary <> OLD.empsalary THEN
INSERT INTO history (updated, oldvalue, newvalue) VALUES('empsalary', OLD.empsalary,NEW.empsalary);
END IF;
END;
///
DELIMITER ;
delimiter $$
create trigger wys_sk_b after update on `dude_base`
for each row
begin
if NEW.salary <> OLD.salary
then
UPDATE `current` s
INNER JOIN things u ON s.id_thing = u.id_thing
INNER JOIN dude_base b ON b.id = s.id
SET s.`curr_cash` = u.thing_cost * b.salary/ 100;
end if;
end;
$$