I have a table salesOrderItems:
soItemID INT(11) PK
salesOrderID INT(11)
productID INT(11)
cost DECIMAL(7,2)
qty TINYINT(2)
extendedCost DECIMAL(8,2)
Via PHP, user is presented with a form to enter productID (via drop down), that selection then populates the cost field. User then enters qty. extendedCost is not visible on form.
What I want is a trigger that should simply auto-calculate/insert extendedCost (i.e. - multiply cost x qty) and populate extendedCost, but I'm getting 1064 errors.
Here's the trigger - what the hell am I missing??? I swear I've done this 20 times:
DELIMITER $$
DROP TRIGGER IF EXISTS EXTENDEDCOST $$
CREATE TRIGGER EXTENDEDCOST AFTER INSERT ON salesOrderItems FOR EACH ROW BEGIN
UPDATE salesOrderItems
SET extendedCost = (cost * qty)
WHERE soItemID = NEW.soItemID;
END $$
DELIMITER ;
Result:
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 '-db' at line 1
SQL Statement:
USE <my db/schema>
ERROR: Error when running failback script. Details follow.
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 '-db' at line 1
SQL Statement:
USE <my db/schema>
I'm sure it's something stupid, but it's driving me nuts and I'm completely stuck and need extra eyes...
Thanks guys, in advance!
Since you are trying to insert a calculated value, why perform an extra update and not set trigger time BEFORE INSERT :
CREATE TRIGGER EXTENDEDCOST BEFORE INSERT ON salesOrderItems FOR EACH ROW BEGIN
SET NEW.extendedCost = NEW.cost * NEW.qty
END $$
See also this answer mysql 'after insert' trigger to calculate a field based on other fields .
Related
I have a Reservation table and am creating a BEFORE INSERT trigger that checks whether a user's reservation is already in the database, i.e. prevent him from making duplicate reservations. This is done to signal the front-end that the user is unable to make a duplicate reservation.
However, mySQL raises this error: ERROR 1064 (42000) at line 3: 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 5. For some reason when the body between BEGIN and END is empty, it doesn't raise an error.
CREATE TABLE reservation (
customer_user_id integer,
date integer,
time integer,
name varchar(100)
);
CREATE TRIGGER trigger_name
BEFORE INSERT -- error raised in this line
ON reservation FOR EACH ROW
BEGIN
DECLARE sum INT;
SELECT COUNT(*)
INTO sum
FROM reservation r
WHERE (r.customer_user_id = new.customer_user_id)
AND (r.date = new.date)
AND (r.time = new.time);
IF sum = 0 THEN
INSERT INTO reservation values (new.customer_user_id, new.date, new.time, new.name);
ELSE
RAISE NOTICE 'duplicate'
END IF;
END;
Additionally, could this trigger be a constraint on the table Reservation? E.g. (customer_user_id, date, time) UNIQUE of sorts, which would prevent adding of duplicates, but I am clueless on how to "signal" the front-end which in turn prompts the user that they cannot add a duplicate reservation.
Sorry if some parts of the code might be weird, I'm new to SQL and was taught pSQL.
Thanks in advance!
I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following 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 '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;
I am trying to create a trigger where if the no tickets are less than the no of adults then value 1 will be inserted in trig table.But, I am getting an error.The code is:
CREATE TRIGGER ch
BEFORE INSERT
ON packagebooking
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
Begin atomic
insert into trig values(1)
MySQL said: Documentation
#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 'IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Pa' at line 4.
What changes should I do?
Your syntax for trigger creation has an error, place the IF statement inside the FOR EACH ROW as shown below:
CREATE TRIGGER ch BEFORE INSERT
ON `database`.`table`
FOR EACH ROW BEGIN
IF(tick.tickets<packagebooking.Adult
AND tick.Package_id=packagebooking.Package_id)
THEN Begin atomic
insert into trig values(1)
END IF;
END;
I have a table named sample_table and its columns are col_a, col_b, col_c.
I am trying to create a trigger in MySQL 5.6 which will update col_c(as concat(col_a,col_b)) on insertion of a row. Query which I have written for this is:
create trigger trg_sample_table
after insert on sample_table
for each row
begin
set col_c=concat(col_a,col_b)
end;
While running this sql, the error I am getting is:
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 6.
Could any one of you please help me to create a trigger as I need?
You need to make it a before insert. Try this:
create trigger trg_sample_table before insert on sample_table for each row
begin
set new.col_c=concat(new.col_a,new.col_b);
end;
You cannot modify the row that you just inserted in an after insert trigger. If you want to modify it, it needs to be done in a before insert.
I tried writing this small trigger in MySQL,
CREATE TRIGGER `leg` BEFORE INSERT ON `bckoff`
FOR EACH ROW BEGIN
INSERT INTO `bckoff` SET `step`=1;
END;
after which I get this error.. I'm a newbie to MySQL.. so please help me out here..
#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 3
Even after you fix this error - you'll get another one: you cannot modify the table that your trigger was created at.
Btw, this is how you should create this trigger:
delimiter |
CREATE TRIGGER `leg` BEFORE INSERT ON `bckoff`
FOR EACH ROW BEGIN
INSERT INTO `bckoff` SET `step`=1;
END;
|
delimiter ;