Create MySQL Trigger manually using VB.NET Application (Systax Error) - mysql

I'm new with MySQL Triggers.
I would like to create MySQL Triggers programmatically using my VB.NET Application so first I create a query using MySQL Workbench test it and it was a success but when I copied the query to my VB.NET Application and put it on string variable and tried to execute it. the error pops up that I have a Syntax error.
DELIMITER $$
CREATE TRIGGER `updateProductPrice`
BEFORE UPDATE ON `list_events`
FOR EACH ROW
BEGIN
IF NEW.caption <> OLD.caption
THEN
SET NEW.caption = 1;
END IF ;
END$$
DELIMITER ;

As P.Salmon said don't use DELIMITER
Use only
CREATE TRIGGER `updateProductPrice`
BEFORE UPDATE ON `list_events`
FOR EACH ROW
BEGIN
IF NEW.caption <> OLD.caption
THEN
SET NEW.caption = 1;
END IF ;
END
as content for your string variable.

Related

Why is this trigger not being created?

I am trying to get this mysql trigger to work in mysql workbench. It will happily tell me when there is an error, but the minute everything appears ok it doesn't run. I've run a show triggers query and nothing is returned. Running v8.0.28.
delimiter //
CREATE TRIGGER add_job_item
AFTER INSERT ON estimate_line
FOR EACH ROW
BEGIN
IF (NEW.CoreTypeID = 3 AND NEW.CoreResourceID IS NOT NULL) THEN BEGIN
INSERT INTO job_items (EstimateLineID) VALUES (NEW.EstimateLineID);
END; # END IF; here doesn't work
END;// # I have tried END; END;//
delimiter ;
Oddly (and I've left it here) the ;// is actually given as an example on the Mysql documentation but errors when I run it (https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html).
I tested this on MySQL 8.0.31 and it works:
delimiter //
CREATE TRIGGER add_job_item
AFTER INSERT ON estimate_line
FOR EACH ROW
BEGIN
IF (NEW.CoreTypeID = 3 AND NEW.CoreResourceID IS NOT NULL) THEN
INSERT INTO job_items (EstimateLineID) VALUES (NEW.EstimateLineID);
END IF;
END//
delimiter ;
Differences:
IF must be ended by END IF;.
You don't need a BEGIN...END within an IF block (unless you need to use DECLARE in the block). It's already a compound statement, and accepts a list of statements in the block.
You do need to use the current delimiter after the last END. That is, END// in this example.

MySQL Version for right syntax

I am getting this error on the SQL schema. The error message i am getting is
"
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'CREATE TRIGGER updtrigger_r4_balance AFTER UPDATE ON
resellers4 FOR EACH R' at line 2"
What syntax is going wrong? I am attaching 2 SQL schema. Both having same problem.
delimiter // DROP TRIGGER IF EXISTS updtrigger_r4_balance;
CREATE TRIGGER updtrigger_r4_balance AFTER
UPDATE
ON resellers4 FOR EACH ROW
BEGIN
IF NEW.callsLimit <= 0
THEN
UPDATE
resellers4_child r4c
INNER JOIN
resellers3 r3
ON (r4c.reseller3_id = r3.id)
SET
r4c.reseller3_callsLimit = r4c.reseller3_callsLimit + r3.callsLimit, r3.callsLimit = 0
WHERE
r4c.reseller4_id = new.id;
END
IF;
END
// delimiter ;
Another one is:
delimiter //
DROP TRIGGER
IF EXISTS updtrigger_r4_balance_add;
CREATE TRIGGER updtrigger_r4_balance_add BEFORE UPDATE ON resellers4 FOR EACH ROW
BEGIN
IF OLD.callsLimit <= 0 THEN
UPDATE resellers3 r3
INNER JOIN resellers4_child r4c ON (r4c.reseller3_id=r3.id)
SET
r3.callsLimit = r3.callsLimit+r4c.reseller3_callsLimit,
r4c.reseller3_callsLimit = 0
WHERE r4c.reseller4_id=new.id;
END IF;
END
//
delimiter ;
After you change the delimiter you have to use it instead of ;
delimiter //
DROP TRIGGER IF EXISTS updtrigger_r4_balance_add; <--- use // instead
MySQL uses ; to separate the queries. Multiple queries can be sent to the server in a single request; the server use the current delimiter (default ;) to split the received text into queries.
There are complex SQL constructs (triggers, stored procedures etc) that may include one or more queries or a BEGIN..END compound statement in their definition.
Because such constructs usually contains two or more queries, separated by the usual delimiter (;), MySQL needs a way to know where the enclosing compound construct ends.
The DELIMITER statement is used to replace the default delimiter (;) with a different one when a compound statement is defined. This allows the standard delimiter (;) to be used inside the body of the compound statement.
It works this way:
The DELIMITER statement is used to change the current delimiter; various values are used as delimiter instead. // is suggested in the documentation but other values can be used too. The only rule is to use a character (or a sequence of characters) that does not appear in the compound statement that is to be defined after it.
From now on, all the subsequent queries must end with the new delimiter (and not with ;).
Declare the complex construct (be it a trigger, stored procedure at event). If it contains more than one statements they have to be separated by ;.
End the complex construct with the delimiter declared on step 1.
Use DELIMITER ; to reset the delimiter. If another statement follows then you have to terminate the DELIMITER statement with the delimiter you set on step 1 (because it is the current delimiter; the new one becomes effective after this statement is parsed and executed).
The solution of your problem is simple: either you use the delimiter you set (//) to separate the DROP TRIGGER and the CREATE TRIGGER statements:
delimiter //
DROP TRIGGER
IF EXISTS updtrigger_r4_balance_add //
CREATE TRIGGER updtrigger_r4_balance_add BEFORE UPDATE ON resellers4 FOR EACH ROW
BEGIN
...
END
//
delimiter ;
Or you move the DROP TRIGGER statement before the DELIMITER statement and leave it as it is (terminated with ;):
DROP TRIGGER
IF EXISTS updtrigger_r4_balance_add;
delimiter //
CREATE TRIGGER updtrigger_r4_balance_add BEFORE UPDATE ON resellers4 FOR EACH ROW
BEGIN
...
END
//
delimiter ;
Also after delimiter changes try to replace word new in statement
r4c.reseller4_id = new.id;
with uppercase one. Because OLD and NEW are MySQL extensions to triggers, so they aren't case sensitive.

Create Update Trigger To Copy Changed Data In MySQL Syntax Error

CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF (OLD.FIDERating <> NEW.FIDERating) THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating)
END;
Hi guys, receiving syntax error at line 6 for some reason, been looking online for solution to what might be the cause. Basically looking a simple trigger, that will copy a change change of FIDERating in Player table to a backup table containing OldFIDERating before it was changed, and the NewFIDERating along with the date change was made.
DELIMITER //
CREATE TRIGGER backupFIDE AFTER UPDATE ON player
FOR EACH ROW
BEGIN
IF NEW.FIDERating <> OLD.FIDERating
THEN
INSERT INTO playerbackup(PlayerName, OldFIDERating, NewFIDERating)
VALUES(OLD.PlayerName, OLD.FIDERating, NEW.NewFIDERating);
END IF;
END;
//
DELIMITER ;

What is the MySQL equivalent of SQL Server's UPDATE() trigger function?

I need help converting this MS SQL update trigger to MySQL. My problem is with converting the MS SQL UPDATE() function, which identifies the specific columns that were updated:
ALTER TRIGGER [dbo].[tran_upd_action] ON [dbo].[tran_action]
FOR UPDATE AS
BEGIN
IF ##ROWCOUNT>0
BEGIN
IF update(column1) OR update(column2)
BEGIN
INSERT into tran_indexerqueue
(trankey, trkey2, tranname) SELECT tran_actionid, 0, 'tranaction' from inserted
END
END
END
This does not have the rowcount check, but the basic trigger syntax will be like this. Note that only the Super User role can create a trigger. Also, you have to change the delimiter first, which is usually a big gotcha for MSSQL DBAs.
DELIMITER $$
CREATE TRIGGER `tran_upd_action` AFTER UPDATE ON `tran_action` FOR EACH ROW BEGIN
INSERT INTO tran_indexerqueue
(trankey, trkey2, tranname)
VALUES(OLD.trankey, OLD.trkey2, OLD.tranname);
END $$
DELIMITER ;

how to call StoredProcedure or Functions from a MySQL Trigger?

I am working on MySQL 5.1.3 and using PHPMyAdmin 3.1.3.1 to access it. With PHP as the Server side scripting Language. My problem statement is can we call a Stored Procedure or Function from the Trigger statement so that when ever an INSERT|UPDATE|DELETE trigger is called, it calls the SP for updating some other tables according to the logic defined.
Look here Mysql Trigger Syntax
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN
-> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter;
It can be easier to define a stored procedure separately and then invoke it from the trigger using a simple CALL statement. This is also advantageous if you want to invoke the same routine from within several triggers.
There are some limitations on what can appear in statements that a trigger executes when activated:
The trigger cannot use the CALL statement to invoke stored procedures that return data to the client or that use dynamic SQL. (Stored procedures are permitted to return data to the trigger through OUT or INOUT parameters.)
The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION, COMMIT, or ROLLBACK.