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
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'm creating a trigger which is supposed to run Query 1 if the value of the field statuscode from the newly inserted row is 200. But if the statuscode is say 300 run Query 2 instead. Is this at all possible in MySQL?
I tried the first thing which came to mind but it's obviously wrong.
CREATE TRIGGER mytrigger AFTER INSERT ON tableA
FOR EACH ROW BEGIN
IF(new.statuscode = 200,
(DELETE FROM tableB WHERE new.guid = tableB.guid),
(UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid))
END
//
DELIMITER ;
The syntax for IF/ELSE in mysql procedures is:
IF (NEW.statuscode = 200) THEN
DELETE FROM tableB WHERE new.guid = tableB.guid;
ELSE
UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid;
END IF;
Here are the docs for trigger syntax:
http://dev.mysql.com/doc/refman/5.5/en/if.html
I think you just have your IF syntax wrong. If statements should have a THEN and ENDIF keyword.
http://dev.mysql.com/doc/refman/5.0/en/if.html
I haven't used MySQL in 6 months or so, so hopefully this syntax is right:
CREATE TRIGGER mytrigger AFTER INSERT ON tableA
FOR EACH ROW BEGIN
IF new.statuscode = 200 THEN
DELETE FROM tableB WHERE new.guid = tableB.guid;
ELSE
UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid;
END IF;
END
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.
i have an stored procedure in mysql like below:
BEGIN
START TRANSACTION;
INSERT INTO tbl1 (v1,v2) VALUES (p1,p2);
UPDATE tbl2 SET s1 = 1 WHERE s2 = s3;
SELECT ROW_COUNT();
COMMIT
END
in this query even the transaction doesn't commit, the row_count will be 1.
in fact my question is that how can i use row_count that it returns 0 if rollback occurred?
I ran into a similar problem recently, but I managed to solve it by selecting the row count inside of the transaction block and then returning it after the commit:
BEGIN
START TRANSACTION;
INSERT INTO tbl1 (v1,v2) VALUES (p1,p2);
UPDATE tbl2 SET s1 = 1 WHERE s2 = s3;
SET #rowCount = (SELECT ROW_COUNT());
COMMIT
SELECT #rowCount;
END
I don't know if this is the best way to solve it, but it seems to work for me :-)
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