What I'm trying to do is this:
Create a trigger named trg_cust_balance that will automatically update the AC_CUSTOMER table’s CUS_BALANCE before a new CHARTER row is added. Use the CHARTER table’s CHAR_TOT_CHG as the update source (Assume that all charter charges are charged to the customer balance.) In addition to the CHAR_TOT_CHG, add $25 for every quart of oil used on the charter.
This is my code:
create trigger trg_cust_balance
before insert on CHARTER
for each row
begin
declare CHG_PRIOR double;
declare OIL_QUANT double;
select CHAR_OIL_QTS into OIL_QUANT
from CHARTER
order by CUS_CODE;
select CHAR_TOT_CHG into CHG_PRIOR
from CHARTER
order by CUS_CODE;
update AC_CUSTOMER
set CUS_BALANCE = CUS_BALANCE + CHG_PRIOR + (OIL_QUANT * 25);
end;
// delimiter ;
So basically what I'm doing here is pulling the quantity of quarts of oil (CHAR_OIL_QTS) and the total charge (CHAR_TOT_CHG) and turning them into variables. Then trying to make the customer balance (CUS_BALANCE) equal to whatever it was plus said charges. However, I keep getting the 1172 error and I don't know why. I suspect it's got to do with the two select statements. Any help would be appreciated, thanks
And you can reduce it to following
But the update clause needs an other WHERE Clause so that the correct custimer gets the balanced changed
drop trigger if exists trg_cust_balance;
delimiter //
create trigger trg_cust_balance
before insert on CHARTER
for each row
begin
update AC_CUSTOMER
set CUS_BALANCE = CUS_BALANCE + NEW.CHAR_TOT_CHG + (NEW.CHAR_OIL_QTS * 25)
where AC_CUSTOMER.CUS_CODE = NEW.CUS_CODE;
end//
delimiter ;
INSERT INTO CHARTER VALUES(10019,'2020-02-10','4278Y','TYS',644,3.8,4.5,167.4,0,10017);
Thsi runs with out a proem seee https://www.db-fiddle.com/f/29eiHwfNHDrL34VQ8CSPAF/1
Related
There are two tables involved in this question: Register(eid, sid, price, rating) and Event (eid, ename, edescription, edate, memprice, nonmemprice, maxpeople).
I want to create a trigger that is monitoring the Register that if the number of registrations of any Event exceed the 80% of the maximum people allowed in such event, we arise the non-memberprice of this event by 50%.
So far I have something like this
delimiter//
create trigger price_change
after insert on Register for each row begin
DECLARE counts INT;
DECLARE maxpeople INT;
set counts = (select count(sid) from Register
JOIN Event ON Register.eid = Event.eid
where eid = new.eid);
set maxpeople = (select maxpeople from Event where Event.eid =
new.eid);
if counts >= 0.8 * maxpeople
then
update Event set nonmemprice = nonmemprice * 1.5 where eid
= new.eid;
end if;
end//
delimiter;
I'm still learning how to write trigger and I ended up with this trigger. I tried to add it into MySQL but it said there are syntax errors basically everywhere of this trigger. I'm really confused. It would be appreciated if anyone help me with this.
The error is right at the beginning. Use a space in between:
delimiter //
^--------------here
And at the end of course too: delimiter ;
I have 3 tables
tbl_payments(pay_id,date,amount,description,-------)
tbl_pay_trans(pay_id,trans_id)
tbl_transactions(trans_id,trans_date,trans_amount,trans_description,-----)
'tbl_transaction' has the same data from the 'tbl_payments' along with some other values. To maintain the relationship between the two tables I use 'tbl_pay_trans'. What I want to do is, when an update done on tbl_payments(amount,description) the same changes need to do in tbl_transactions(trans_amount,trans_description). I wrote a trigger to do that, but it dose not update the tbl_transaction table values as it supposed to.
My trigger is
DELIMITER $$
CREATE TRIGGER update_trans
AFTER UPDATE on tbl_payments
FOR EACH ROW
BEGIN
DECLARE new_amount VARCHAR(50);
DECLARE new_description TEXT;
DECLARE new_pay_id,new_trans_id INT;
SET new_pay_id = OLD.pay_id;
SET new_amount = OLD.amount;
SET new_description = OLD.description;
SELECT trans_id INTO new_trans_id FROM tbl_pay_trans WHERE pay_id = new_pay_id;
UPDATE tbl_transactions SET
trans_amount = new_amount,
trans_description = new_description
WHERE trans_id = new_trans_id;
END$$
DELIMITER ;
Please someone help me to figure out what I did wrong.
It's not updating because you are using OLD on the amount and description columns where you need to use NEW i.e.
SET new_amount = NEW.amount;
SET new_description = NEW.description;
OLD refers to the column value before the update occurred in tbl_payments. See the manual.
I have a table called COURSE with these columns:
CourseId int
CourseName varchar
DateofStart Date
I would like to create a trigger which ensures that courses cannot be run during August.
Check the examples given in this site for writing the trigger
https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm#i1025109
step 1: Since you need to check that courses cannot run during the august, you would be requiring a BEFORE INSERT trigger type.(Check the syntax in the link)
Step 2: Write the logic in Trigger body which will check the date that is being entered in the table
Step 3: If the DateofStart is in august raise an exception or process accordingly
Do try and share the code snippet.
Try this SQL code (for SQL Server)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER Tr_Course
ON Course
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE Course
SET dateOfStart = I.dateOfStart
FROM Course AS C
JOIN INSERTED I ON C.courseID = I.courseID
WHERE MONTH(I.dateOfStart) <> 8
END
GO
create trigger check_month
on COURSE
for Insert
as
declare #Date nvarchar(10)
declare #month int
select #Date=i.DateofStart from inserted i;
set #month= (select DATEPART(MONTH,#Date))
if(#month=8)
begin
Rollback
end
else
begin
commit
end
I did a trigger in mysql to shoot alerts always an input value was less than the set value. But now I need it is done in SQL SERVER.
I would be grateful if someone could help me transform mysql trigger to a SQL Server trigger.
Thanks to all at once.
My trigger is:
DELIMITER $$
create TRIGGER alert
AFTER INSERT ON records
FOR EACH ROW
begin
Set #comp=0;
Set #tempmax=0;
Set #tempmin=0;
select lim_inf_temp into #tempmin from sensores where idSensor=NEW.idSensor;
Set #maxidAlarme=0;
if (CAST(NEW.Temperatura AS UNSIGNED)<#tempmin) then
SELECT MAX(idAlarme) into #maxidAlarme FROM alarmes;
SET #maxidAlarme=#maxidAlarme+1;
INSERT INTO alarmes(idAlarme,descricao_alarme, idRegisto) VALUES (#maxidAlarme,"inserted below the normal temperature",New.idRegisto);
INSERT INTO sensores_tem_alarmes(idSensor,idAlarme,dataAlarme) VALUES (NEW.idSensor,#maxidAlarme,NOW());
set #comp=+1;
end if;
set #id_sensores_em_alerta=1;
SELECT MAX(id_sensores_em_alerta) into #id_sensores_em_alerta FROM sensores_em_alerta;
INSERT INTO sensores_em_alerta(id_sensores_em_alerta, idSensor, idAlarme, data_registo, numerosensoresdisparados) VALUES (id_sensores_em_alerta,NEW.idSensor, #maxidAlarme, NOW(), #comp);
end $$;
DELIMITER ;
I've tried to make the trigger in SQL Server, but as the script is different and I'm getting many difficulties to do the right way.
My attempt that was not going at all well:
CREATE TRIGGER Alert ON registos AFTER INSERT AS
BEGIN
DECLARE #comp decimal= 0
DECLARE #tempmax decimal= 0
DECLARE #tempmin decimal= 0
DECLARE #current_max_idAlarme int = (SELECT MAX(IdAlarme) FROM alarmes)
-- Insert into alarmes from the inserted rows if temperature less than tempmin
INSERT alarmes (IdAlarme, descricao_alarme, idRegisto)
SELECT
ROW_NUMBER() OVER (ORDER BY i.idRegisto) + #current_max_idAlarme,
'temp Error',
i.idRegisto
FROM
inserted AS i
WHERE
i.Temperatura < #tempmin
END
But dont do anything.
Dont create data on table alarmes :S
Does anyone could help me please. I would be eternally grateful.
Many Greetings and thank you all.
First of all, MSSQL doesn't have the option FOR EACH ROW, so it treats multiple inserted rows at once as a set. You will therefore have to insert the values into a table variable.
Unfortunately I do not know much MySQL actually, but I believe this is a starting point?
CREATE TRIGGER ALERT
ON records
AFTER INSERT
AS
BEGIN
DECLARE #comp INT;
DECLARE #tempmax INT;
DECLARE TABLE #tempmin (tempmin INT);
INSERT INTO #tempmin
SELECT s.lim_inf_temp FROM sensores s WHERE s.idSensor IN (inserted.idSensor);
--rest of the code
I'm going to post this code against my better judgement - redesign the tables is better than this hack.
This uses a ROW_number() to virtualise a surrogate identity key for the alarmes table. This is a 'bad plan' (tm).
Also the answer is partial - it doesn't do everything your question asked for -- I hope it gets your further along the road. Use it as a guide for how to interact with the virtual INSERTED table. Good luck
CREATE TRIGGER Alert ON records AFTER INSERT AS
BEGIN
DECLARE #comp INT = 0
DECLARE #tempmax INT = 0
DECLARE #tempmin INT = 0
-- get the max current id.
-- note that this is EXTREMELY unsafe as if two pieces of code are executing
-- at the same time then you *will* end up with key conflicts.
-- you could use SERIALIZABLE.... but better would be to redisn the schema
DECLARE #current_max_idAlarme = (SELECT MAX(IdAlarme) FROM alarmes)
-- Insert into alarmes from the inserted rows if temperature less than tempmin
INSERT alarmes (IdAlarme, descricao_alarme, idRegisto)
SELECT
ROW_NUMBER() OVER (ORDER BY i.idRegisto) + #current_max_idAlarme,
'temp Error',
i.idRegisto
FROM
inserted AS i
WHERE
i.Temperatura < #tempmin
END
I'm fairly new to SQL in general and even more so to MySQL and I've hit a stumbling block. I'm attempting to use a procedure to copy the value of one field to another if the original field is not null, this procedure is then called by triggers whenever the table is updated or has a new row inserted into it. Here is what I have so far:
-- WORK_NOTES_PROCEDURE - This copies the contents of the estimate notes to the work order notes if the original estimate had any notes with it.
DROP PROCEDURE IF EXISTS 'WORK_NOTES_PROCEDURE';
DELIMITER $$
CREATE PROCEDURE WORK_NOTES_PROCEDURE()
BEGIN
DECLARE var_temp VARCHAR(50);
SET var_temp := (SELECT ESTIMATE_NOTES FROM ESTIMATES WHERE ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER);
IF var_temp IS NOT NULL THEN
UPDATE WORK_ORDERS SET WORK_ORDER_NOTES = var_temp WHERE WORK_ORDERS.ESTIMATE NUMBER = ESTIMATES.ESTIMATE_NUMBER;
END IF;
END$$
DELIMITER ;
Absolutely any help would be appreciated, the error I'm getting is a syntax error for the line where I'm assigning a value to var_temp.
try,
SET var_temp = (SELECT ESTIMATE_NOTES
FROM ESTIMATES INNER JOIN WORK_ORDERS
ON ESTIMATES.ESTIMATE_NUMBER = WORK_ORDERS.ESTIMATE_NUMBER
LIMIT 1);