new to Triggers, i created one that works when i hard code the value that i actually need to list as a variable. as in the code, table tbl_measure_stats has trigger StatsTrigger. When tbl_measure_stats changes, it sets tbl_activity.override = 0 WHERE tbl_activity.activityid is supposed to equal the tbl_measure_status.actid. The value in the trigger table is tbl_measures_stats.actid, which is supposed to equal the id of tbl_activity.activityid, whose value override i need to set to 0. How do I code the variable related to 54321? Code is actually simpler than my explanation:
USE [TestDB]
GO
/****** OBJECT: Trigger [dbo].[StatsTrigger] ******/
/****** PURPOSE: detect any field changes in tbl_measure_stats ******/
/****** ... then set tbl_activity.override = 0 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
ALTER TRIGGER [dbo].[StatsTrigger] --resides in tbl_measure_stats>Triggers
ON tbl_measure_stats --table which has the trigger
AFTER INSERT,DELETE,UPDATE --when something changes...
AS UPDATE tbl_activity set override = 0 where tbl_activity.activityid = 54321
seems like this should be a straight forward solution but i am finding nothing actionable in all my searches. OLD, NEW, inserted for example... i am not getting those to work. Example when trying "inserted":
AS UPDATE cpy_activity set porc = 0 where cpy_activity.activityid = inserted.actid
returns the error:
The multi-part identifier "inserted.actid" could not be bound.
ideas?
You probably need to include INSERTED in the FROM clause of the query.
ALTER TRIGGER [dbo].[StatsTrigger] --resides in tbl_measure_stats>Triggers
ON tbl_measure_stats --table which has the trigger
AFTER INSERT,DELETE,UPDATE --when something changes...
AS
UPDATE A SET override = 0
FROM tbl_Activity A JOIN INSERTED I ON A.activityID = I.actid
Note however that INSERTED won't have any rows when the trigger fires because of a DELETE action. You should check whether INSERTED or DELETED have rows and do the right thing accordingly.
Related
I need to write a SQL trigger whenever (engage_step_user_response) table (response_json)
column updated, I need to increase no of retakes by one in the retakes column. I have tried to write a SQL trigger referencing some details. But it doesn't succeed. can anyone please help regarding this?
CREATE TRIGGER increment_engage
ON engage_step_user_response
AFTER UPDATE
AS
IF UPDATE(response_json)
FOR EACH ROW
SET NEW.retakes = OLD.retakes + 1
I'm expecting whenever the response_json column is updated retakes must be increase by one.enter image description here
Try this :
You will need a BEFORE UPDATE trigger if you want to modify the data:
CREATE TRIGGER increment_engage BEFORE UPDATE ON engage_step_user_response
FOR EACH ROW
BEGIN
IF !(NEW.response_json <=> OLD.response_json) THEN
SET NEW.retakes = NEW.retakes+1;
END IF;
end;
demo here
CREATE TRIGGER increment_engage
BEFORE UPDATE ON engage_step_user_response
FOR EACH ROW
SET NEW.retakes = OLD.retakes + NOT (NEW.response_json <=> OLD.response_json);
the code is null-safe.
I've been trying to create a MySQL Trigger in order to set a random value to a column called confcode which is used for authentication purposes.
The issue is, the value never sets after I insert a new row and keeps being empty.
I use phpMyAdmin to create the trigger, and these are the defines:
Trigger name: confcode
Table: ebaysales
Time: BEFORE/AFTER (both don't work)
Event: INSERT
Definition: SET #confcode = FLOOR(RAND()*999999)+111111
The trigger itself gets inserted successfully, but doesn't seem to affect anything..
I guess you want:
SET NEW.confcode = FLOOR(RAND()*999999)+111111;
-- instead of
SET #confcode = FLOOR(RAND()*999999)+111111;
I am trying to add a trigger to my SQL DB from phpMyAdmin.
When applying the trigger:
CREATE TRIGGER `download_url` AFTER INSERT ON
`tbl_files` FOR EACH ROW UPDATE tbl_files SET
download = CONCAT('http://website/', url)
WHERE 1
When trying to upload a file, I get no results; if I remove the trigger it functions properly. I need the download column to update with the prefix [http://website/] and value [url].
Thank you!!!
You can use a considerably simpler approach - instead of having an AFTER INSERT trigger, use a BEFORE INSERT trigger and manipulate the incoming row before writing it to the table. You can use the special variable NEW to reference this new row:
CREATE TRIGGER download_url
BEFORE INSERT ON tbl_files
FOR EACH ROW
SET NEW.download = CONCAT('http://website', NEW.url);
END;
I'm trying to calculate the percentage column in my table project after an update occurs on any of the following columns fund and goal.
This is the query I made up:
CREATE TRIGGER percentcalc AFTER UPDATE ON project
FOR EACH ROW
SET percent = ( fund / goal ) *100
but I seem to get an error:
#1193 - Unknown system variable 'percent'
delimiter //
CREATE TRIGGER percentcalc AFTER UPDATE ON project
FOR EACH ROW BEGIN
SET NEW.percent = ( NEW.fund / NEW.goal ) * 100;
END
//
delimiter ;
There are two major problems:
You have to use NEW keyword to access values of columns of a row that is being updated
You can't change values of NEW variables in an AFTER trigger. If you try to do so you'll get 'Updating of NEW row is not allowed in after trigger' error. Therefore you have to use only BEFORE event for your trigger
Trigger Syntax and Examples
...Within the trigger body, the OLD and NEW keywords enable you to
access columns in the rows affected by a trigger. (OLD and NEW are not
case sensitive.)...
...In a BEFORE trigger, you can also change its value
with SET NEW.col_name = value if you have the UPDATE privilege for it.
This means you can use a trigger to modify the values to be inserted
into a new row or used to update a row. (Such a SET statement has no
effect in an AFTER trigger because the row change will have already
occurred.)...
That being said your trigger should look like
CREATE TRIGGER percentcalc
BEFORE UPDATE ON project
FOR EACH ROW
SET NEW.percent = (NEW.fund / NEW.goal) * 100;
Here is SQLFiddle demo
New to MySql triggers, just learning.
CREATE TRIGGER MyTrigger
AFTER UPDATE ON MyTable
FOR EACH ROW
BEGIN
IF (new.field1 < 0 or new.field1 > 5) THEN
UPDATE new SET new.field1 = old.field1;
END IF;
END;
The goal is to keep the value of field1 the same, if the update puts it outside the range.
However, instead it sets it to 0. What am I doing wrong? How should this code look?
Here is an example that should hopefully get you started:
DELIMITER ~
CREATE TRIGGER `so_13547992_trigger`
BEFORE UPDATE ON `so_13547992`
FOR EACH ROW BEGIN
IF ( NEW.`field1` < 0 OR NEW.`field1` > 5 ) THEN
SET NEW.`field1` = OLD.`field1`;
END IF;
END;
~
Why would it work better? Well first of all your example trigger is recursive, you can't update the same table in a trigger that was triggered by an update.
Second, the new in your UPDATE statement is not a table name, you need to specify one explicitly.
It doesn't appear to be a legit trigger at all, doesn't your server complain when you try to create it? Can you perhaps show actually SHOW CREATE TRIGGER `your_trigger`; to make sure that it's really created and looks like you pasted it above?
Even if your example would would work, you're trying to do an unconstrained update on all rows of your table, not on the ones you're trying to update, you should have a WHERE clause; again, given that issue one and two are taken care of.