What i'm doing wrong?
CREATE TRIGGER `Calc` AFTER INSERT on `test`.`bookings` FOR EACH ROW
BEGIN
UPDATE `test`.`article` AS `ST`
SET `ST`.`stock` = SUM(`test`.`bookings`.`amount`)
WHERE `ST`.`articlenr` = `test`.`bookings`.`NEW.article`;
END
The idea is, to calculate "STOCK" in ARTICLE, after an insert like:
Amount: 1 Article: 123
on table bookings.
How can this be solved?
WHERE `ST`.`articlenr` = NEW.`article`;
New its not a column
=(SELECT SUM(`test`.`bookings`.`amount`) FROM `test`.`bookings` GROUP BY article)
Related
new to MySQL and databases and need some help.
I have the tables:
appointment
appointment_id int PK , <-
patient_id int ,
doctor_id int , <-
appointment_time DateTime.
queue
appointment_id int PK, <-
actual_time DateTime.
queue_summary
date datetime ,
doctor_id int PK , <-
num_of_patients int. <-
I need to write a trigger that will update the num_of_patients by adding +1 to the record in the row that the doctor_id in the queue_summary is overlapping with the new row inserted into queue that corresponds to the appointment (to extract the doctor_id).
ex. in case of insertion to queue:
appointment table status:
need to update:
I wrote the following select query to extract the doctor_id that was affected by the insertion/deletion to queue:
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id;
and I tried to write the trigger as followed:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
But with no luck...
I know I have some syntax error in the where part but I can't manage it to work...
please advise,
thanks in advance!
The SET command inside triggers can only be used on NEW var, which points to the row that is being processed. To update another table, you need to issue a normal UPDATE command:
delimiter //
CREATE TRIGGER tr_insert
BEFORE INSERT ON queue
FOR EACH ROW
BEGIN
update queue_summary
set queue_summary.num_of_patients = queue_summary.num_of_patients+1
where queue_summary.doctor_id = (
select appointment.doctor_id
from appointment as a
join queue as q
on a.appointment_id = q.appointment_id
where new.appointment_id = a.appointment_id
)
END;//
delimiter ;
Thats the reason why appers a syntax error on where, it's because mysql set command wont expect it there, SET only accepts var name and value.
I'm trying to create a trigger to give "SumBeforeTaxes(from 'order')" a value.
The "SumBeforeTaxes" is itemQuantity(from 'order_item') x price ('from itemsinstock').
So if one has ordered 3 items at 10 USD each, the "SumBeforeTaxes" would be 3 x 10 (30 in total). Likewise, if one has ordered 3 items at 10 USD each AND 2 items at 15 each, the "SumBeforeTaxes" should be 3x10 + 2x15 (60 in total).
Here's what I've tried so far - but I keep getting syntax errors. I'm also not sure my triggers (despite the syntax errors) are correct.
1st attempt:
CREATE DEFINER = CURRENT_USER TRIGGER `orderdb`.`order_AFTER_INSERT`
AFTER INSERT ON `order_item` FOR EACH ROW
BEGIN
SET getItemPrice = (SELECT Price FROM itemsinstock);
INSERT orderdb.'order'
SET SumBeforeTax = itemQuantity * getItemPrice;
END
2nd attempt:
CREATE DEFINER = CURRENT_USER TRIGGER `orderdb`.`order_AFTER_INSERT`
AFTER INSERT ON `order_item` FOR EACH ROW
BEGIN
SET getItemPrice = (SELECT Price FROM itemsinstock);
INSERT INTO orderdb.'order' (SumBeforeTax) VALUES (new.itemQuantity * getItemPrice);
END
3rd attempt:
CREATE DEFINER = CURRENT_USER TRIGGER `orderdb`.`order_AFTER_INSERT`
AFTER INSERT ON `order_item` FOR EACH ROW
BEGIN
declare getItemPrice DOUBLE;
SET getItemPrice = (SELECT Price FROM itemsinstock WHERE ItemID = new.itemID);
INSERT INTO orderdb.'order'(SumBeforeTax) VALUES (itemQuantity * getItemPrice);
END
Can I get a little help on this one?
order is a reserve word in MySQL and thus you need to escape it like below using backtique for your INSERT statement.
INSERT INTO orderdb.`order`(SumBeforeTax) VALUES (itemQuantity * getItemPrice);
For any syntactical clarification refer Documentation On CREATE TRIGGER Syntax.
You might want to change your code like below in that case using an UPDATE statement rather:
BEGIN
declare getItemPrice DOUBLE;
SELECT Price INTO getItemPrice FROM itemsinstock WHERE ItemID = new.itemID;
UPDATE orderdb.`order` SET SumBeforeTax = new.itemQuantity * getItemPrice
WHERE OrderID = 101;
I have 2 tables:
table 1 = SAMPLE_TABLE
table 2 = RESULT_TABLE (with proposed trigger)
I would like to use a trigger in RESULT table that, when a new record is inserted into into it, will update a field in SAMPLE table called, 'status' = "complete". The field 'status' to be updated in SAMPLE is related to RESULT by:
table 1 = SAMPLE_TABLE ('client_sampleID')
=
table 2 = RESULT_TABLE ('sampleID')
This is the proposed trigger
CREATE DEFINER = `user`#`%` TRIGGER `database`.`RESULT_TABLE_BEFORE_INSERT`
AFTER INSERT ON `RESULT_TABLE` FOR EACH ROW
BEGIN
UPDATE SAMPLE_TABLE
SET status = 'complete'
WHERE SAMPLE_TABLE.client_sampleID = RESULT_TABLE.sampleID;
END
My questions:
is this above trigger OK?
there are 100+ 'client_sampleID' (all same, entered as a batch) per 'sampleID'. Is there a more efficient way of setting the 'status' so that it happens only after encountering the first instance?
You are very close. You just need to use new in the trigger:
CREATE DEFINER = `user`#`%` TRIGGER `database`.`RESULT_TABLE_BEFORE_INSERT`
AFTER INSERT ON `RESULT_TABLE` FOR EACH ROW
BEGIN
UPDATE SAMPLE_TABLE st
SET status = 'complete'
WHERE st.client_sampleID = new.sampleID;
END
I have a requirement where i have to create a trigger when there is an update in "psoprdefn" table for specific roles from "psroleuser" and insert values into my customized table.
For some reason the below trigger does'nt seem to work. Please help.
CREATE TRIGGER z_LOGIN2
AFTER UPDATE OF lastsignondttm ON SYSADM.PSOPRDEFN
FOR EACH ROW
BEGIN
SELECT DISTINCT a.oprid, a.oprdefndesc, a.lastsignondttm, b.rolename
from SYSADM.psoprdefn a, SYSADM.psroleuser b
where a.oprid=b.roleuser
and (rolename = 'FAS Change Admin' or b.rolename='PeopleSoft Administrator')
INSERT INTO z_LOGIN VALUES(psoprdefn.oprid,psoprdefn.oprdefndesc,psoprdefn.lastlogondttm)
Try something like this:
CREATE TRIGGER z_LOGIN2 AFTER UPDATE OF lastsignondttm ON SYSADM.PSOPRDEFN
FOR EACH ROW
BEGIN
IF (SELECT COUNT(1)
FROM SYSADM.psroleuser b
WHERE b.roleuser = NEW.oprid AND (b.rolename = 'FAS Change Admin'
OR b.rolename = 'PeopleSoft Administrator')) > 0 THEN
INSERT INTO z_LOGIN VALUES (NEW.oprid,NEW.oprdefndesc,NEW.lastlogondttm);
END IF;
END;
This validates if the user that updated the original table has a rolename of 'FAS Change Admin' or 'PeopleSoft Administrator' in psroleuser. If he does, it inserts the updated values in the z_LOGIN table.
I cannot find any solution for this 'problem'.
My database needs to update a field in one table when another table is updated.
(only when the new value is 5 or 7 and the new prop.key is "status_id")
But the updated table doesn't include the values that I need. It only contains an ID to another table.
I am not sure about the syntax for this statement. This one won't work. Can anybode help me?
This is what I tried:
delimiter $$
CREATE TRIGGER autoClosedOn AFTER INSERT ON journal_details
FOR EACH ROW BEGIN
IF
NEW.prop_key = "status_id"
AND NEW.value = "5"
OR NEW.value = "7"
THEN
UPDATE
issues i
JOIN journals j
ON j.journalized_id = i.id
SET
i.closed_on = j.created_on;
WHERE j.id = NEW.jounal_id;
END IF;
END$$
delimiter ;
You have a superfluous semicolon before the WHERE clause of your update statement.