SQL Trigger solve - mysql

CREATE TRIGGER Print
Before UPDATE ON employ
FOR EACH ROW
WHEN (NEW.Employe_ID>0)
DECLARE
salary int;
BEGIN
salary:= :NEW.salary-:OLD.salary;
dbms_output.put('Old salary:'||:OLD.salary);
dbms_output.put('New salary:'||:NEW.salary);
dbms_output.put_line('Difference'||salary);
END;
/
Shows
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 'WHEN (NEW.Employe_ID>0)
DECLARE
salary int' at line 4
I have a table name employe which has 4 columns Employe_ID, E_name,
Department_ID,
salary
What is the problem with this sql and how can I solve it?

I don't get why you added the condition on employee_id > 0, but in case you actually need it
delimiter //
CREATE OR REPLACE TRIGGER print BEFORE UPDATE ON employee
FOR EACH ROW
IF ( NEW.employe_id > 0 ) THEN
SELECT NEW.salary - OLD.salary INTO #delta;
END IF;
//
delimiter ;
Insert and update your data, then read #delta
insert into employee select 1,'Me',1,2400;
update employee set salary = 2800 where id = 1;
Select #delta;
I don't think it's possible to automatically output #delta to the screen as it would be on Oracle using dbms_output.put_line().

Related

MySQL syntax error while creating a UPDATE Trigger

I am trying to create a trigger on updating the salary field of this table.
customers(ID, Name, Age, Address, Salary) from this site SQL Trigger
While creating it shows the following error
MySQL said:
#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 'OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
' at line 2
Here is the code snippet:
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN `enter code here`
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END
Note that, I'm using phpMyAdmin. Does dbms_ouput.put_line() works there?
As said in the comments, you are using Oracle syntax, it can't work on MySQL.
Then, there's no reason to have this trigger on DELETE and INSERT because your aim is to compute the salary difference on a specific row before and after it get updated. You can't have a value NEW on DELETE, and you can't have a value OLD on INSERT.
Thus your computation is only meaningful on UPDATE.
Here's the correct MySQL syntax (I am assuming that you have a column sal_diff on your table)
DELIMITER $$
CREATE TRIGGER `update_sal_diff`
BEFORE UPDATE ON `customers `
FOR EACH ROW
BEGIN
SET NEW.sal_diff = NEW.salary - OLD.salary;
END $$
DELIMITER ;
If this is not what you are trying to achieve, edit your question and add clear requirements

How to decrement a value in another table via a trigger? (MySQL)

I want my trigger to decrement the quantity by 1 in the table "quantity" when a new row is added to "rented_equipment_log" where date_returned has a value of NULL.
A basic synopsis of the database is that there is an equipment table with columns; model, maker, type and quantity. (E.g. 1001, 'Jackson', 'oar', 10)
The table rented_equipment_log has the columns; member_id, model, type, date_taken, date_returned. (E.g. 17225663, 1001, oar, 2018-11-26, 2018-11-27)
So when a member takes out a piece of equipment but has not yet returned it (date_returned is null), the quantity of the same model decrements by 1 in the table equipment.
However I'm getting a syntax error. I've looked at other questions similar to this and still can't figure out what the error is.
Here's the trigger:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW BEGIN
DECLARE q integer;
SELECT quantity INTO q FROM equipment WHERE model = NEW.model;
IF (date_returned IS NULL) THEN
UPDATE equipment
SET quantity = q -1 WHERE model = NEW.model
END IF;
END;//
And 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 IF;
END' at line 9
I'm not sure why you are getting an error there. But your query is more complicated than necessary:
delimiter //
CREATE TRIGGER UpdateQuantity
AFTER INSERT ON rented_equipment_log
FOR EACH ROW
BEGIN
UPDATE equipment e
SET e.quantity = e.quantity - 1
WHERE e.model = NEW.model and ?.date_returned IS NULL;
END;//
I'm not sure if date_returned should be new.date_returned or e.date_returned. I don't even understand that condition. I would be expecting e.equipment > 0.

MySQl trigger error on insert and update same table (Error Code: 1064)

TRIGGER `dn_name`.`schedule_arrive` AFTER INSERT
ON `dn_name`.`administration_schedule`
FOR EACH ROW BEGIN
DECLARE approx_hr INT(11), update_hr DATETIME;
SELECT approx_hr FROM `administration_routes` WHERE id = NEW.route_id INTO approx_hr;
SELECT DATE_ADD(new.depart_time, INTERVAL approx_hr HOUR_MINUTE) INTO update_hr;
UPDATE administration_schedule SET arrive_time=approx_hr WHERE id = NEW.id;
END$$
On running insert query, I designed to select approx hour from route table with related id and set that value to approx_hr, increase datetime from new inserted depart time and assign to update_hr and finally update the arrive_time field in trigger enabled same table. But on executing my query, the system show as follows;
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 ', update_hr datetime;
select approx_hr from administration_routes where id =' at line 7
About the NEW.myColumn and OLD.myColumn, you can only use them when using "BEFORE" something. After an insert, you don't have an OLD.myColumn or NEW.myColumn gut a current one. for what you want to do, use "BEFORE INSERT" and change your "SELECT,... SELECT..., UPDATE..." to a simple
SET NEW.arrive_time = DATE_ADD(NEW.depart_time,INTERVAL ...);
Use a before update trigger:
CREATE TRIGGER `dn_name`.`schedule_arrive` BEFORE INSERT
ON `dn_name`.`administration_schedule`
FOR EACH ROW
BEGIN
DECLARE approx_hr INT(11), update_hr DATETIME;
SELECT approx_hr FROM `administration_routes` WHERE id = NEW.route_id INTO approx_hr;
SELECT DATE_ADD(new.depart_time, INTERVAL approx_hr HOUR_MINUTE) INTO update_hr;
SET NEW.arrive_time = approx_hr;
END$$

SQL If Statment / select into in a stored procedure

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)

MySQL Trigger Syntax Error union two/three tables

I wrote following code:
create trigger money after update on `things`
for each row
begin
Select #c1=sum(`thing_cost`) from `things`
UNION
Select #c2=sum(`salary`) from `dude_base`
Update `current` set `curr_cash`=#c1*#c2/100
end;
$$
Table "things" has got:
id1 (PK)
name
thing_cost
Table dude_base has got:
id2 (PK)
salary
name, etc. irrevelant
Table current has got:
id1 (FK)
id2(FK)
curr_cash
I got 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 'Update `current` set `curr_cash`=#c1*#c2/100; END' at line 7
Any help?
I think you should have a semicolon ; like
Select #c2=sum(`salary`) from `dude_base`;
to end this query because UPDATE is another query
// edit
try it like this
SET #c1 = (SELECT sum(`thing_cost`) from `things`);
SET #c2 = (SELECT sum(`salary`) from `dude_base`);
UPDATE `current` SET `curr_cash` = #c1 * #c2 / 100