I'm using SQLite 3.37.2 through Python 3.10.5, and editing the database with DBeaver - maybe it's not always at its best when reporting database errors.
Anyway, I want to convert a text (JSON) in SQLite proper JSON format upon insertion, by calling its json() function.
I thought about adding a simple trigger to handle that, but I can't figure out what's wrong in my syntax:
CREATE TRIGGER t1_before_insert AFTER INSERT ON t1 FOR EACH ROW
BEGIN
SET NEW.json:=json(NEW.json);
END
;
This is the latest version I attempted, I always get an error like
SQL error or missing database (near "SET": syntax error)
I tried:
SET NEW.json:=json(NEW.json)
SET NEW.json=json(NEW.json)
SELECT NEW.json:=json(NEW.json)
using an AFTER INSERT trigger
but none worked.
You can't change the value of a column of the new row like that.
You must update the table in an AFTER INSERT trigger:
CREATE TRIGGER t1_after_insert AFTER INSERT ON t1 FOR EACH ROW
BEGIN
UPDATE t1
SET json = json(NEW.json)
WHERE t1.id = NEW.id;
END;
Change id to the table's primary key.
See the demo.
Related
So I have this trigger that I wrote for a MySQL environment, and which I now need to transfer to a SQL Server environment.
Being unfamiliar with Transact SQL, I have a little trouble translating from one to the other or creating an equivalent. Here is the simplified query:
CREATE TRIGGER <myTrigger> BEFORE INSERT ON <myTable>
IF NEW.<myColumnContainingBoolean> = TRUE THEN
SET NEW.<myColumnReferenceCode> = CONCAT(YEAR(NOW()),MONTH(NOW()),DAY(NOW()), 'indice');
ENDIF;
The goal is to add a reference number (today's date writted yyyymmdd + 'indice') according to the value of a boolean contained in the query, to summarize, if, at the time of the INSERT, the value of the boolean is on TRUE then we insert the code on this same line, otherwise we don't write a reference. Here is a maybe more explicit example :
Example
I have sincerely tried a lot of things, what seems to come closest to my request is this one (which, of course, does not work):
CREATE TRIGGER <myTrigger>
ON <myTable>
AFTER INSERT
AS
BEGIN
IF <myColumnContainingBoolean>
SET <myColumnReferenceCode> = CONCAT(YEAR(GETDATE()),MONTH(GETDATE()),DAY(GETDATE()), 'indice');
FROM inserted
END
GO
Ok, I guess we have to use a trigger (immense sigh).
Here's how you would do it in SQL Server:
CREATE TRIGGER <your schema>.<your table>_Insert ON <your schema>.<your table>
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO <your schema>.<your table> (<your other columns>,<myColumnReferenceCode>)
SELECT
<your other columns>
,CASE
WHEN <myColumnContainingBoolean> = 1 THEN FORMAT(GETDATE(),'yyyyMMdd') + 'indice'
ELSE <myColumnReferenceCode>
END
FROM
inserted
END
GO
If you're using an auto-incremented (IDENTITY) column, make sure to leave it off your insert list inside the trigger.
Other observations: You could probably just make <myColumnReferenceCode> a date and store GETDATE() and get the same functionality, but I don't know all of your circumstances.
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've been trying to create a simple BEFORE INSERT trigger on a database table (MySQL v 5.7 ) but I keep receiving a vague "#1064 ... syntax error" message which doesn't help resolve the issue.
Here's the SQL:
CREATE OR REPLACE TRIGGER `CREATE_QUIZ_TRIG` BEFORE INSERT ON `quiz`
FOR EACH ROW BEGIN
SET NEW.ACTIVE = UPPER(NEW.ACTIVE);
SET NEW.CREATED = NOW();
END
/
All I'm trying to do is enforce a column to uppercase and then insert the current date & time into a timestamp column. I've been following the documentation from:
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
and realise that for multi-statement expression I have to redefine the delimiter at the beginning of the trigger's creation but the same '#1064' error occurs.
This is made even more confusing because when I use phpmyadmin's interface for creating the same trigger it works fine - but won't when I export the generated SQL and try to create the trigger using that!?
Thanks for any help
I didn't realise that, by default, phpmyadmin adds a ; delimiter which was breaking the ; used to end a statement within the BEGIN END block.
I am trying to create a trigger in mysql using the following:
CREATE TRIGGER ins_daft BEFORE INSERT ON jos_ezrealty
FOR EACH ROW BEGIN
SET preschool = livingarea*10.76391041671
END;
When I do I get the following error:
Error
SQL query:
CREATE TRIGGER ins_daft BEFORE INSERT ON jos_ezrealty
FOR EACH
ROW BEGIN
SET preschool = livingarea * 10.76391041671 END
MySQL said: Documentation
#1193 - Unknown system variable 'preschool'
I am trying to have the value of one field converted to square feet by multiplying by 10.76391041671. Can anyone see what I am doing wrong?
Thank you.
Any time you want to reference the columns of a row that fired the trigger, qualify them like NEW.column_name.
Otherwise the SET command thinks you want to set a MySQL configuration variable called preschool.
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;