I want to create trigger 'after insert' on table1
If i insert record in Table1
it will check corresponding ID in table2 and update the status of
corresponding ID in table2.
there is additional condition on table2.
status of that ID should be Null.
my attempt so far.
but its not working
CREATE TRIGGER 'table1_AFTER_INSERT` AFTER INSERT ON `table1` FOR EACH ROW
BEGIN
update table2 a
set a.status= 'coordination pass',
where a.ID = new.ID and a.status is Null;
END
You have many sintax errors like comma after set a.status= 'coordination pass', or in trigger name definition etc. Here is the correct triger sintax
CREATE TRIGGER `table1_AFTER_INSERT` AFTER INSERT ON `table1`
FOR EACH ROW BEGIN
UPDATE `table2` a
SET a.status= 'coordination pass'
WHERE a.ID = NEW.ID AND a.status is NULL;
END
As i seen your trigger there is two error first is with table name you use ' single quote and second is set a.status= 'coordination pass', comma at last
CREATE TRIGGER table1_AFTER_INSERT
AFTER INSERT ON table1`
FOR EACH ROW
BEGIN
UPDATE table2 a
SET a.status= 'coordination pass'
WHERE a.ID = new.ID AND a.status is Null;
END;
Related
Drop TRIGGER if exists triggername;
DELIMITER $$
CREATE TRIGGER triggername
AFTER UPDATE ON table1 FOR EACH ROW
BEGIN
IF (
Select person from table1 e
JOIN table1Type et ON e.table1TypeID = et.table1TypeID
where et.Description = 'University Degree' and e.Active = 1)
THEN update table2
set field = 1 ;
ELSE update table2 set field = 0;
END IF;
END$$
DELIMITER ;
I am trying to use a conditional statement to update another table if that condition is met.
So in other words, if the person in table 1 has a type of university degree and is active then update another field in table 2.
CREATE TRIGGER triggername
AFTER UPDATE
ON table1
FOR EACH ROW
UPDATE table2
SET field = EXISTS (SELECT NULL
FROM table1 e
JOIN table1Type et USING (table1TypeID)
WHERE et.Description = 'University Degree'
AND e.Active = 1);
Maybe you must use NOT EXISTS (the logic is unclear).
PS. DELIMITER not needed for this trigger code.
I would like to make a SQL Trigger that will evaluate a query like this:
IF NOT (SELECT * FROM table1 WHERE table1.id=1 AND table1.finished=0)
DO
UPDATE table2 SET finished=1 WHERE table2.id=table1.id
That would tell me that all rows from table1 with an id of 1 are finished (if there are no unfinished(0) rows) and, if so, it should update table2 and set the value finished to 1.
Can anyone help me with the Trigger structure? I'm new when it comes to that.
You can do it this way
DELIMITER //
CREATE TRIGGER tg_au_table1
AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
IF NOT OLD.finished <=> NEW.finished THEN
UPDATE table2 t
SET finished = (EXISTS(SELECT *
FROM table1
WHERE id = t.id
AND finished = 0))
WHERE id = NEW.id;
END IF;
END//
DELIMITER ;
Here is SQLFiddle demo
Extended answer is provided in your other question https://stackoverflow.com/a/21270582/1920232
I have two tables: table1 and table2, which triggers on inserts and on updates in the same function.
As you insert a value in table1 or table2 a value is inserted in table3, with the value table1.lastname || table1.firstname assigned to column3. The id obtained for the insert in table3 must be inserted into table1.id_table3.
CREATE OR REPLACE FUNCTION myschema.myfunction() RETURNS trigger AS
$BODY$
DECLARE
new_id_table_4 integer;
BEGIN
IF TG_OP = 'INSERT' THEN
IF TG_TABLE_NAME = 'table1' THEN
new_id_table_4 := 1;
ELSIF TG_TABLE_NAME = 'table2' THEN
new_id_table_4 := 2;
END IF;
INSERT INTO myschema.table3
(id, id_table4, name)
VALUES (DEFAULT, new_id_table_4, NEW.columnA||', '||NEW.columnB, TRUE, TRUE)
RETURNING id
INTO NEW.id_table3;
ELSIF TG_OP = 'UPDATE' THEN
IF OLD.columnA <> NEW.columnA OR OLD.columnB <> NEW.columnB THEN
UPDATE myschema.table3 SET
name = NEW.columnA||', '||NEW.columnB
WHERE id = NEW.id_cuenta;
END IF;
END IF;
RETURN NEW;
END
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION myschema.myfunction() OWNER TO myuser;
CREATE TRIGGER add_table3record_table1
AFTER INSERT OR UPDATE
ON myschema.table1
FOR EACH ROW
EXECUTE PROCEDURE myschema.myfunction();
CREATE TRIGGER add_table3record_table2
AFTER INSERT OR UPDATE
ON myschema.table2
FOR EACH ROW
EXECUTE PROCEDURE myschema.myfunction();
The problem is that when I insert a new record into table1 or table2,
...RETURNING id INTO NEW.id_table3;
It does not seem to have any effect.
This is my first function/trigger ever, and I cannot find the error.
Thank you!
I'm pretty sure you can't update a row AFTER it has already been inserted just by setting NEW.foo = bar.
Either:
Perform an update on the table1 setting the new id_table3 value (which is going to recursively call you ON UPDATE trigger, so be careful), or
Use a BEFORE trigger instead of an AFTER.
Depending on how your foreign keys are set up between table1 and table3, the latter may or may not be an option.
Question is create a trigger that will only update a record if the old record is different from the new. Can i get some help please i don't even know how to start the syntax apart from
CREATE TRIGGER update_marketingliste
ON marketing_list FOR UPDATE
AS
BEGIN
SELECT * FROM INSERTED
END
I am not sure what the schema is but the sample code below should help you to understand how this can be done.
SET NOCOUNT ON
GO
CREATE TABLE test(col1 int primary key , col2 int )
go
CREATE TRIGGER trg_test_upd on test
INSTEAD OF UPDATE
AS
BEGIN
IF EXISTS (
SELECT *
FROM Deleted del -- old value
INNER JOIN Inserted ins -- new value
on del.col1 = ins.col1
and del.col2 = ins.col2)
BEGIN
PRINT 'No Update'
RETURN --if old value is same as new value dont update
END
ELSE
BEGIN
UPDATE t1
set col2 = i.col2
from dbo.test t1
inner join inserted i
on t1.col1 = i.col1
END
END
go
Insert into test (col1, col2)
select 10, 10
go
update test
set col2 = 200
where col1 = 10
SELECT *
FROM TEST
--This would not do the update
update test
set col2 = 200
GO
Drop table test
go
i have a tow table name t1,t2 . Based on the value insert in t1 i want to insert in t2 .
i use the following trigger .
create trigger testt after insert on t1
BEGIN
for each row
if NEW.uid='Mill' then insert into t2 (uid2) values (NEW.uid)
end if
if NEW.uid='Farm' then insert into t2 (uid2) values (NEW.r)
end if
END
Please help me to resolve this issue . Thanks in advance
It looks like you just have some keywords out of order:
CREATE TRIGGER ...
FOR EACH ROW
BEGIN
...
END
create trigger testt after insert on t1
BEGIN
for each row BEGIN
if NEW.uid='Mill' then insert into t2 (uid2) values (NEW.uid)
end if
if NEW.uid='Farm' then insert into t2 (uid2) values (NEW.r)
end if
END