i have a site where i can update the date of a row, by clicking on the table, when i change the value and press enter its updated in the database.
what i want: in the date field. just to type mm/dd (e.g. 05-20) press enter.
then my trigger should:
CREATE TRIGGER before_transactions_update
BEFORE UPDATE
ON Transactions FOR EACH ROW
BEGIN
-- variable declarations
declare inputDate integer;
-- trigger code
-- get month and day
-- LEFT(inputDate, 2); RIGHT(inputDate, 2)
-- STR_TO_DATE('2015,MONTH(LEFT),DAY(RIGHT)','%Y,%m,%d');
SET INSERTED.date = 2015-month-day-00-00-00;
END;
this is what i've "tried" or at least as far as i have come on my own. MySQL workbench stops me at the declaration of my var, it tells me i need a semi colon after declaration, which i dont seem to need
So i've been googling for hours and i've met a dead end as a newbie in MySQL so could anyone fill out the blanks or point me in the right direction,
that would be great
You can not pass the input parameter directly into the trigger unless that input is somewhere in the update statement
Also you need to specify a delimiter in the trigger.
If your input values are always in the form m-d as you shown in the example and the year is the current year then the following trigger should do the job.
delimiter //
CREATE TRIGGER before_transactions_update BEFORE UPDATE
ON Transactions
FOR EACH ROW
BEGIN
if length(new.date) = 5 then
set new.date = concat(year(curdate()),'-',new.date);
end if ;
END;//
delimiter ;
Related
I'm new to the php mysql developpement, I want to make a trigger to be launched after I insert a row in the evolution table. The trigger must take a value (prixMisDaccord) from another table (inscription) and reduce it value from the evolution column prixAPaye.
Here is what I tried and what I found on Stack Overflow:
DELIMITER $$
CREATE TRIGGER trg_rap
BEFORE INSERT ON evolution FOR EACH ROW
BEGIN
DECLARE pmd float;
-- Check BookingRequest table
SELECT prixMisDaccord
INTO #pmd
FROM inscription
WHERE inscription.idETD= 1;
SET NEW.resteAPaye = #pmd-NEW.prixPaye
WHERE idETD = 1;
END;
$$
DELIMITER `;
'i have a probleme from this line SELECT' - Is not the error I get, I do get an error on the set statement because you cannot apply a where clause to a set..There are other problems with your code and you don't seem to know the difference between user defined variables and declared variables see - How to declare a variable in MySQL? and temporary tables..so #pmd-NEW.prixPaye is just nonsense.
If you want more help read https://stackoverflow.com/help/how-to-ask and provide table definitions,sample data and desired outcome all as text in the question.
create trigger cal_retweet before insert on T
for each row begin
set NEW.retweet_change = NEW.retweet_count - retweet_count where id_str = NEW.id_str
end
SQL said there is syntax error near "where id_str = NEW.id_str"
My table looks like this. Where id_str is a unique identifier for a specific tweet. Since I am inserting 50 tweets from a single user every minute, there would be many same id_str. What I want to look at is the change of retweet_count every minute. tweeted_at is when the user tweeted, created_at is when this data is inserted into my database. I want to generate retweet_change for each new data inserted into the database compared to the same old tweet (into the column retweet_change). How should I write the trigger?
After reading some of your comments I changed my code to :
create trigger cal_retweet before update on T
for each row
begin
set NEW.retweet_change = NEW.retweet_count - OLD.retweet_count;
end;
There is still syntax error
There are several issues with this trigger.
You have some syntax errors. You need proper semicolons to delimit your statements.
You have a WHERE statement that is out of place (and actually not needed). You are acting on only a single row at a time, you don't have to match on the id_str.
In order to factor in a calculation using an existing value from the row, you need access to the OLD keyword. For that, you need a trigger that happens on UPDATE, not INSERT. On INSERT, the retweet_change is simply the same as retweet_count; you could alter your INSERT statement to fix that problem.
You may need to explicitly add a statement delimiter as per the comments below.
So all together, I think this trigger should look like:
DELIMITER //
CREATE TRIGGER cal_retweet BEFORE UPDATE ON T
FOR EACH ROW
BEGIN
SET NEW.retweet_change = NEW.retweet_count - OLD.retweet_count;
END;//
DELIMITER ;
I have an issue which i'm hoping someone out there will be able to assist.
I'm trying to write a trigger which takes the datetime stamp and puts just the date into a new column. But likewise, also if a value in a different column is equal to x, then replace it with y.
I can get individual statements to work (i.e if I have just the date code or the replacement code individually it works fine), but I can't get them to both work in the same trigger.
SET NEW.date = LEFT(NEW.entrydate, 10);
IF
(NEW.connect_ip = "1.2.3.4")
THEN SET
NEW.connect_ip = "0.0.0.0";
END IF
Multi-statement triggers need BEGIN and END around their "body", and usually DELIMITER overridden around their declaration.
Example/Template:
DELIMITER $$
CREATE TRIGGER [blah blah]
BEGIN
[do stuff]
END$$
DELIMITER ;
Alternatively, for your specific case, I noticed an answer the other day with a possiblity I had overlooked; you can set multiple things in a SET statement:
SET NEW.date = LEFT(NEW.entrydate, 10)
, NEW.connect_ip = IF(NEW.connect_ip = "1.2.3.4", "0.0.0.0", "1.2.3.4")
;
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.
I am seeking a short term solution while I work out why a synchronisation is setting one field wrong in a table
I prepared a trigger and would welcome some comment on it, and any necessary corrections or better strategies.
CREATE TRIGGER urlcorrect AFTER INSERT ON sym_node
FOR EACH ROW BEGIN
IF NEW.sync_url= 'http://wrongaddress' THEN
UPDATE sym_node SET sync_url= "http://123.456.7.89:1234/etc";
END IF;;
END$
delimiter;
thanks
David
Your trigger is wrong in various ways.
First of all, I think you want a BEFORE trigger so that you can fix the row before it gets into your table.
Secondly, this:
UPDATE sym_node SET sync_url= "http://123.456.7.89:1234/etc";
would update every sync_url in the sym_node table and that's not what you want. And I don't think MySQL will let you UPDATE a table inside a trigger on that table (someone correct me if I'm wrong on this please). Also, you should be using single quotes for string literals even though MySQL will let you use double quotes, don't pick up bad habits from MySQL lax behavior. You want to:
set new.sync_url = 'http://123.456.7.89:1234/etc';
Putting all that together, you get this:
delimiter $
create trigger urlcorrect before insert on sym_node
for each row begin
if new.sync_url = 'http://wrongaddress' then
set new.sync_url = 'http://123.456.7.89:1234/etc';
end if;
end;
$
delimiter ;