SQL where syntax error with trigger - mysql

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 ;

Related

MySQL trigger error doesn't work because it needs a value from another table column

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.

I want to write trigger for restricing numeric values insertion

I want to write trigger for restricting user to insert numeric values and special characters in Name field of column.
CREATE TRIGGER trig_check BEFORE
INSERT ON tempuser
FOR EACH ROW
BEGIN
IF :new.firstname NOT LIKE '%[0-9]%'
THEN
dbms_output.put_line('INSERT ONLY ALPHABETS');
END IF;
END;
/
As #a_horse_with_no_name pointed out, better to add a check constraint like the one below :
alter table TAB
add constraint CHK_NAME_WITHOUT_NUMBER
check (not regexp_like(name,'[0-9]+'));
of course after clearing the data which contain numbers. To accomplish this aim, the following DML statement may be used before the above DDL :
update tab
set name = regexp_replace(name,'[0-9]+','');

Unexpected END_OF_INPUT in MySQL trigger

I have searched for all the possible online solutions but I can't figure out the error in this trigger.
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END;
the first error appears at OLD.neat_link
syntax error, unexpected END_OF_INPUT, expecting ';'
and the second one at END;
syntax error, unexpected END
Any help would be appreciable, thanks.
That problem is due to interpreting individual statements. The CREATE TRIGGER statement is as such a single complete statement that must be sent as is to the server. Usually statement borders are recognized by the default delimiter (the semicolon). In case of stored programs however the semicolon is needed to separate inner statements. This would confuse the client as it cannot tell apart what is an inner statement of the stored program or a full statement as it must be sent as a whole to the server.
Hence the DELIMITER statement was introduced which only applies to clients (not the server, the server itself cannot parse this statement). It changes the default delimiter to one of your choice, leading so the client where to look for the statement's end. A typical case hence looks like this:
DELIMITER ;;
CREATE TRIGGER `ins_film` AFTER INSERT ON `film` FOR EACH ROW BEGIN
INSERT INTO film_text (film_id, title, description)
VALUES (new.film_id, new.title, new.description);
END;;
Their is only one statement in the body of the Trigger, so there is no need to use the BEGIN-END compound statement construct. Try this:
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`
another possible solution
DELIMITER $$
CREATE TRIGGER `delete_neat_link`
AFTER DELETE ON `neat_urls`
FOR EACH ROW
BEGIN
DELETE FROM `css_paths`
WHERE `css_paths`.`path_id` = OLD.`neat_link`;
END$$
DELIMITER ;

Not allowed to return a result set from a trigger

I need to increment the next highest character field upon an insert - e.g. if '007' exists, then next is '008'
I have a stored procedure:
BEGIN
SELECT LPAD(CAST(MAX(Line_Order) AS SIGNED INTEGER) + 1,3,'0')
FROM bill_project_lineitems
WHERE Item_Id = vItem;
END
Then I have this trigger:
CALL MaxLineOrder(new.item_id,#new.line_order )
Which I call before.
When I try to INSERT, MySQL complains: 1415 - 'Not allowed to return a result set from a trigger'
So then how do I solve this problem?
Ok, I read again and MAYBE I undestood. (but why not pasting the full code?) The error is clear, You CANT return a resultset from a Trigger.
I suppose that you want to issue an INSERT and get a resultset. But you can't. So you should:
Move your INSERT into the Stored Procedure
DROP the Trigger
CALL the Procedure.
Stored Procedures can return a resultset (or even many resultsets), so this will work. I hope that this is what you want.

update trigger to maintain correct url in field

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 ;