error 1064 in trigger mysql - mysql

This is my first time using mysql and I am tying to learn how to use trigger.
Im using navicat, i go to table design and then go triggers tab. I create a trigger named testing and in definition I typed:
delimiter |
CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload
FOR EACH ROW BEGIN
set new.lpr_text := reverse(new.lpr_text);
END;
| delimiter ;
All im tying to do is whenever something new is inserted, I reverse the text in lpr_text field. However, i get "1064 - you have an error in your SQL; check the manual that corresponds o your MySql server version for the right syntax to yse 'ON lpr_opt_upload' FOR EACH ROW create trigger testing before insert on lpr_op' at line 1." I dont understand what Im doing wrong, I am just copying an trigger example.
//-------------------------------------------------------------------------//
I figured out the problem. I am using navicat and in navicat trigger tab, you only type the body into the definition, Not the header (ex: CREATE TRIGGER lpr.mytesting AFTER INSERT ON lpr.lpr_opt_upload). There are check box next to the name of your trigger and you use those instead of writing your own header.

The DELIMITER command is a client command, which not all clients support (it doesn't get sent to the server at all, it just instructs the client how to tell statements apart in order that they get sent to the server correctly). References to it in the MySQL manual assume that you are using the 'official' MySQL clients, such as the mysql command-line tool or MySQL Workbench (both of which support the DELIMITER command).
I don't know how to change the statement delimiter in Navicat, but that is the root of your problem.

Related

Run stored procedure included sql script using robot framework

I want to run sql script which include db and table creations and stored procedure creations.
but when I try to run sql script using execute sql script keyword in database library I get an error like below
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELIMITER $$\n CREATE OR
REPLACE PROCEDURE `proc_GetCustomerDetails`(\n I...' at line 2")
before procedure I have delimiter like this,
DELIMITER $$
CREATE OR REPLACE PROCEDURE `proc_GetCustomerDetails`(
IN CustomerNbr LONGTEXT,
IN Lang VARCHAR(5)
)
DETERMINISTIC
BEGIN
IF Lang IS NULL THEN SET lang = "fin";
END IF;
SELECT * from dbname.customer;
END;$$
DELIMITER ;
If I comment stored procedure part, sql file is running without errors with rest of the table creation statements.
I googled this and couldn't find any related issue. I saw we have call stored procedure keyword. but I want to keep table creations and stored procedures in same sql file and need to run. I use MariaDB for this task.
Libraries used,
pymysql
robotframework-databaselibrary
If I run sql file using HeidiSQL it is running without any errors with procedures and delimiters. That mean there are no sql errors.
Can Someone tell me how to fix this?
DELIMITER is a statement supported only for the client, it is not supported by the server; thus the error. The solution - drop it.
Here's a question with very good answers what is it and why it's needed.
In short - when you work with a client you need a way to instruct it "this is not a statement you should execute immediately, this is still just a line in the SP you'll be sending to the server" - so you tell (to the client) "the DELIMITER b/n statements is temporarily $$". The server doesn't need/care about that - it knows everything between CREATE PROCEDURE, BEGIN, END are connected statements, a block.
When you connect to the DB through API (pymysql) vs an interactive client (shell, heidisql, etc) - you're sending the SP as a block, there's no way its statements will be ran one by one, thus the DELIMITER is not needed, not a supported command by the server, and generates an error. Drop it.

SQL Trigger on database to restrict drop/alter/create syntax error, despite following tutorial, ON is not valid at this position?

Working on a security DDL trigger for a database and want to restrict non-admins from creating/dropping database, I have created this trigger and put it below all the other code (create tables etc) in the database script.
CREATE TRIGGER alert_table
ON DATABASE
FOR CREATE_TABLE, DROP_TABLE, ALTER_TABLE
AS
BEGIN
IF IS_MEMBER ('admin') = 0
BEGIN
PRINT 'Please contact your Database Admin'
ROLLBACK TRANSACTION;
END
END
GO
CREATE
Note:
USER 'admin'#'localhost'
It says "ON is not valid at that position, expecting: BEFORE or AFTER", and something similar for "END". Thanks!
You followed a tutorial for Microsoft SQL Server, commonly abbreviated as mssql server, while you are using mysql.
Mssql server has DDL triggers which syntax follows the one in your question.
Mysql's create trigger syntax is different and does not have DDL triggers at all, so you cannot use triggers to handle table creation events.

MySQL Not Reading Full Line

I'm using the following SQL Script in my IDE DBeaver, MySQL 8.0.21 Linux (Docker Container). Database is in utf8mb4 / utf8mb4_general_ci encodings.
DELIMITER //
CREATE OR REPLACE TRIGGER trg_line_total
BEFORE INSERT ON LINE
FOR EACH ROW
BEGIN
SET NEW.LINE_TOTAL = NEW.LINE_UNITS * NEW.LINE_PRICE;
END //
DELIMITER ;
It seems to be valid SQL, but it is returning the following error, as if it wasn't reading the full line. I can remove tabs, line returns and it will read more or less characters.
Error occurred during SQL script execution
Reason:
SQL Error [1064] [42000]: 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 'TRIGGER trg_line_total
BEFORE INSERT ON LINE
FOR EACH ROW
BEGIN
SET NEW.LINE_TO' at line 1
I already seemingly fixed this error once today by deleting everything I had in my editor and typing it out from scratch, as if there was some invisible line ending that was messing things up or getting interpreted. I looked in vim and used the :set list command and I see are regular line return carriages. What could it be?
Unfortunately, there's no ALTER TRIGGER or CREATE OR REPLACE TRIGGER in Oracle's MySQL. It supports only Create Trigger format.
For MariaDB, in version 10.1.4, added support CREATE OR REPLACE TRIGGER to their drop-in replacement for MySQL.
If you are sure that trigger doesn't exists
Use CREATE instead of CREATE OR REPLACE.
If you are modifying existing trigger
I would suggest that the best practice is to lock the table where the trigger lives, so no rows are impacted with the trigger absent. Dropping and adding triggers while a table is locked is allowed.
mysql> LOCK TABLES t1 WRITE; -- the next prompt appears once you've obtained the lock
mysql> DROP TRIGGER t1_bi;
mysql> DELIMITER $$
mysql> CREATE TRIGGER ti_bi BEFORE INSERT ON t1 FOR EACH ROW
BEGIN
...
END $$
mysql> DELIMITER ;
mysql> UNLOCK TABLES;
Reference: Modify Existing Trigger Definition in MySQL

MySQL Workbench "OLD" syntax changed?

I had a trigger set in MySQL workbench that used to accomplish what I wanted. Recently, my workplace updated MySQL workbench to version 6.3.10 across all computers and the triggers were dropped during the update for some reason.
Basically, the trigger should update a column with the current date when a different column is changed. This was previously achieved by using the following syntax:
if (NEW.colname <> OLD.colname or (OLD.colname is null && NEW.colname is not null))
then
set NEW.othercolname = current_date()
The issue is when I try to recreate the same trigger in the new MySQL version and apply it, I am met with the error:
ERROR 1363: There is no OLD row in on INSERT trigger
It seems to me that MySQL is looking for a row named OLD, which does not exist; however, this syntax used to work in previous versions for the intended result. Can anyone suggest an alternative syntax to accomplish this?
First of all, the error message comes from mysql, not from mysql workbench, so mysql workbench's version is irrelevant here.
Secondly, the error message is pretty clear: you are trying to refer to the OLD row in an insert trigger. This is obviously impossible, since there is no old row when you insert a new one.
You need to change either the trigger to catch an update event or change the trigger logic.
ERROR 1363: There is no OLD row in on INSERT trigger
You've got MySQL Workbench set to create an INSERT trigger. As the error message says, there's no OLD row in an INSERT trigger.
What you want is an UPDATE trigger.

MS Access - Syntax error on CREATE TABLE Statement in MySql - using Trigger

For some reason, i can't get this to work..
CREATE TRIGGER triggerupdate
INSTEAD OF UPDATE ON ORDERDETAILS
AS
IF UPDATE(ORDERVALUE) BEGIN
Print ('INSTEAD OF Trigger [triggerupdate] - Trigger executed!!')
Print('You cannot update Order Value')
END
I'm trying to do a trigger if someone tries to update OrderValue. IT kept saying that Syntax error on Create Table.
Let me know what I have wrong.
Thanks
You can't use MS Access. Access does not support TRIGGERs, that's why you're getting Syntax errors, because "CREATE TRIGGER" is not supported SQL in Access.
Also, Print isn't a function in Access SQL either.
It's possible that you're meant to use Access as a front-end for MSSQL Server, which can be done, and where your SQL statements will become valid (but your use of Print is archaic).