Good day all,
I am trying to keep some historical test data if a certain field changes after every test. I have the data in a table called gypsum and wish to track changes in a table called history_gypsum.
The trigger I have created shows errors which I do not understand,your help will be appreciated.Many thanks.G Styles
DELIMITER $$
CREATE TRIGGER `pattesti_testing`.`history_gypsum`
BEFORE UPDATE
ON `pattesti_testing`.`gypsum`
FOR EACH ROW
BEGIN
IF OLD.leakage != NEW.leakage
THEN
INSERT INTO history_gypsum
(
id,
register_number,
equipment_description,
make,
class,
location,
continuity_value,
insulation_value,
leakage,
polarity,
test_frequency,
date_of_test,
comments,
passed,
tester_initials,
next_test,
current,
)
VALUES
(
NEW.id,
NEW.register_number,
NEW.equipment_description,
NEW.make,
NEW.class,
NEW.location,
NEW.continuity_value,
NEW.insulation_value,
NEW.leakage,
NEW.polarity,
NEW.test_frequency,
NEW.date_of_test,
NEW.comments,
NEW.passed,
NEW.tester_initials,
NEW.next_test,
NEW.current,
);
END IF ;
END;
$$
DELIMITER ;
ERRORS
Query : CREATE TRIGGER `pattesti_testing`.`history_gypsum` BEFORE UPDATE ON `pattesti_testing`.`gypsum` FOR EACH ROW BEGIN ...
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 ')
VALUES
(
NEW.id,
NEW.register_n' at line 26
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
Remove the comma after current
current, /*this comma is too much*/
)
VALUES
Related
I have trigger statement but its not working
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW BEGIN
INSERT INTO table3_seq VALUES (NULL);
SET NEW.option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0'));
But it shows an error like 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 3
I am assuming you are trying to update a sequence when an insert is done. See this(http://en.latindevelopers.com/ivancp/2012/custom-auto-increment-values/).
The below code snippet may work.
CREATE TRIGGER tg_table3_insert BEFORE INSERT ON rd_stonepanel_pricing
FOR EACH ROW
BEGIN
INSERT INTO table3_seq SET option_id = CONCAT('R', LPAD(LAST_INSERT_ID(), 3, '0');
END;
Remove the semicolon at line 3 if it is a single statement and add end. Also, the above code will only work if the table "table3_seq" has one column.
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
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.
CREATE TRIGGER x AFTER INSERT ON itemtype
FOR EACH ROW
DECLARE
minn itemtype.PRICE%type;
BEGIN
select MIN(itemtype.PRICE) into minn from itemtype;
IF (:new.PRICE > minn*4) then RAISERROR('Custom text');
END IF;
END;
/
I'm trying to create a trigger that raises an error when I try to insert a new entry into the itemtype with itemtype.PRICE column value is greater than 4 times the current low priced item on the table.
I get these compilation errors when I try to create the trigger.
LINE/COL ERROR
-------- --------------------------------------------------------------
5/31 PL/SQL: Statement ignored
5/31 PLS-00201: identifier 'RAISERROR' must be declared
I have also tried
CREATE TRIGGER x AFTER INSERT ON itemtype
FOR EACH ROW
DECLARE
minn itemtype.PRICE%type;
BEGIN
select MIN(itemtype.PRICE) into minn from itemtype;
if (:new.PRICE > minn*4) then raise_application_error(-20010,'Too Expensive');
END IF;
END;
/
which complies, but when i try to insert a new entry into the table I get theses errors saying my trigger fails.
SQL> insert into itemtype(ITEMNUM,NAME,PICTURE,PRICE,BELONGSTO ) VALUES ('A11','The who knows','',10.99,'P');
insert into itemtype(ITEMNUM,NAME,PICTURE,PRICE,BELONGSTO ) VALUES ('A11','The who knows','',10.99,'P')
*
ERROR at line 1:
ORA-04091: table USERNAME.ITEMTYPE is mutating, trigger/function may not see it
ORA-06512: at "USERNAME.X", line 5
ORA-04088: error during execution of trigger 'USERNAME.X'
Try use PRAGMA AUTONOMOUS_TRANSACTION in trigger.
Look my answer on this question:
SQL trigger on delete mutating table
Currently trying to have a generic activity log table that stores which table, field, value changed (+ necessary primary key)
DELIMITER $$
CREATE TRIGGER tr_customers_insert_activity_log AFTER INSERT ON `customers`
FOR EACH ROW
BEGIN
DECLARE curr_column CHAR(255);
DECLARE finished INT DEFAULT false;
DECLARE column_name_cursor CURSOR FOR SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'customers' ORDER BY ordinal_position;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET finished = 1;
OPEN column_name_cursor;
column_loop: LOOP
IF finished THEN
LEAVE column_loop;
END IF;
FETCH column_name_cursor INTO curr_column;
INSERT INTO activity_log(`cid`, `table`, `field`, `value`, `modified_by`, `modified_at`)
VALUES (NEW.cid, 'customers', curr_column, NEW.#curr_column, NEW.modified_by, NEW.modified_at);
END LOOP column_loop;
CLOSE column_name_cursor;
END$$
DELIMITER ;
The problem I have is in here:
INSERT INTO activity_log(`cid`, `table`, `field`, `value`, `modified_by`, `modified_at`)
VALUES (NEW.cid, 'customers', curr_column, NEW.#curr_column, NEW.modified_by, NEW.modified_at);
Since I am dynamically looping through each field by name I don't know how I can get the NEW.#curr_column value. How can you access a property of the NEW/OLD objects using the value of a variable?
To clarify the syntax error is:
#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 '#curr_column, NEW.modified_by, NEW.modified_at); END LOOP column_loop' at line 17
Thanks!
It's not possible to dynamically address the NEW and OLD values within a TRIGGER.
We can use a CASE expression. But this probably isn't what you were looking for. It's not really "dynamic". We need to statically address each column name we're interested in.
CASE curr_column
WHEN 'cid' THEN NEW.cid
WHEN 'foo' THEN NEW.foo
WHEN 'othercol' THEN NEW.othercol
END
Also problematic is the various datatypes of the columns you might want to store in activity_log table value column... DATE, INTEGER, DECIMAL, ENUM, VARCHAR, ... those are all going to need to be cast to a single datatype of the value column.
Some alternatives to consider:
have the trigger save a copy of the entire row
rather than making the trigger "dynamic", make the creation of the trigger more dynamic... i.e. use a SELECT from information_schema.columns to assist in producing the contents needed in the trigger definition