I want to add a new function to an existing MariaDB Database. This is the update script:
DELIMITER //
CREATE FUNCTION second_diff(dateColA timestamp, dateColB timestamp) RETURNS INT
BEGIN
DECLARE diff INT;
if dateColB is null then
SET diff = TIMESTAMPDIFF(SECOND, dateColA, current_timestamp);
else
SET diff = TIMESTAMPDIFF(SECOND, dateColA, dateColB);
end if;
RETURN diff;
END;
//
DELIMITER ;
UPDATE version SET current_version = '1.0.1';
When I am running it, it fails with the following errors:
Error executing: DELIMITER //
CREATE FUNCTION second_diff(dateColA timestamp, dateColB timestamp) RETURNS INT
BEGIN
DECLARE diff INT
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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 //
else
SET diff = TIMESTAMPDIFF(SECOND, dateColA, dateColB)
Error executing: if dateColB is null then
SET diff = TIMESTAMPDIFF(SECOND, dateColA, current_timestamp)
. Cause: java.sql.SQLException: (conn=1720684) Unknown system variable 'diff'
Error executing: else
SET diff = TIMESTAMPDIFF(SECOND, dateColA, dateColB)
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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 'else
end if
Error executing: end if
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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 'end if' at line 1
RETURN diff
Error executing: RETURN diff
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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 'RETURN diff' at line 1
END
Error executing: END
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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 'END' at line 1
//
DELIMITER
Error executing: DELIMITER
. Cause: java.sql.SQLSyntaxErrorException: (conn=1720684) 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' at line 1
UPDATE version SET current_version = '1.0.1'
EDIT: This only fails when I am running this migration script at a different server. On my local machine everything is working fine. I was already checking the version numbers of both different MariaDBs. They are both the same.
I found the issue. When running the migration script at the different server, mybatis was used to execute it. According to this issue, you need a special syntax when using Delimiter. So instead of using
DELIMITER // and DELIMITER ;
it was necessary to write
-- #DELIMITER //and -- #DELIMITER ;
Related
Good evening
I am trying to insert data from one table to another in my script and it comes up with database not found - have I done something wrong?
mysql -e INSERT INTO mysql.db SELECT * from "$cpuser"_mysql.db;
CPUser is a variable manually assigned earlier in the script.
From the CLI I can select * from both
Output from comment;
ERROR 1064 (42000) at line 1: 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 ''domain7mysql4'_mysql.db' at line 1
ERROR 1064 (42000) at line 1: 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 ''domain7mysql4'_mysql.user' at line 1
-se expects a string, so you need to provide one
mysql -e "INSERT INTO mysql.db SELECT * from `${cpuser}_mysql`.db;"
or
mysql -e "INSERT INTO mysql.db SELECT * from ${cpuser}_mysql.db;"
if there are no reserved text
New to SQL and trying to run this code and I get the error "#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 '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.
I am trying to set the trigger in maria db
The query which I am running is
CREATE TRIGGER TEMP
AFTER INSERT
ON sample_details FOR EACH ROW
BEGIN
UPDATE SAMPLE_MONITOR SET TableDataCount= TableDataCount+ 1,Time=NOW() WHERE
TableName='sample_details';
END; //
delimiter ;
I have a table named 'sample_details' there If any insert is done to that table, I want to increment the count in the row present in SAMPLE_MONITOR table by 1 and also insert the current time
But I am getting the following error after running query
ERROR 1064 (42000): 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 '' at line 1
So anyone know how to solve this?
The question I used to help build this transaction is here:
How to add 1 hour to currrent_timestamp in mysql which is the default value?
I'm trying to create a transaction in my database to fill in the allowed column in my mariadb database table blacklisted_ips. I want the allowed column to be an hour after my added column which has a default value of CURRENT_TIMESTAMP. Here is my transaction so far:
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END;
The error message I'm getting is the following:
#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 '' at line 2
Here is my table schema:
CREATE TABLE blacklisted_ips (
ip_id int(11) NOT NULL AUTO_INCREMENT,
ip_add varchar(15) NOT NULL,
added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
allowed timestamp NOT NULL,
PRIMARY KEY (ip_id)
);
In MySQL, there's an ambiguity between the ; that terminates a CREATE TRIGGER statement, and the possible ; characters that terminate individual statements in the body of the trigger.
The error you got is confusing:
#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 '' at line 2
Normally a syntax error includes the text of the statement following the position where the syntax parser got confused. But in your case, it got confused at ;, thinking it was the end of the CREATE TRIGGER statement. Therefore the error occurs at the termination; there is no text following the error as far as the parser is concerned.
The same issue affects CREATE PROCEDURE and CREATE FUNCTION.
To fix the ambiguity, MySQL client supports a builtin command to change the DELIMITER, so you can change it to something distinct from any sequence of characters that appear in the body of your routine.
DELIMITER ;;
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW BEGIN
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
END ;;
Alternatively, since in your case the trigger is a single-statement trigger, you don't need a BEGIN...END block at all. This way you can skip changing the DELIMITER, because the ; that terminates your CREATE TRIGGER is the same ; that terminates the single statement of the trigger.
CREATE TRIGGER before_insert_on_blacklisted_ips BEFORE INSERT ON blacklisted_ips
FOR EACH ROW
SET NEW.allowed=NOW()+INTERVAL 1 HOUR;
P.S.: This is documented with an example here: https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
I'm new to sql so bear with me, but I'm trying to create an event that will populate the row of a table at a specific time with values from 1 to x where x is a pre-declared variable. I'm attempting to use a delimiter for the first time and I'm not really sure where all my semi-colons should be at but the following is what I assumed after a while of reading:
DELIMITER $$
DECLARE day_m INT DEFAULT 0;
SET day_m = DATE_FORMAT(CURRENT_DATE, '%e');
WHILE day_m <= DATE_FORMAT(LAST_DAY(CURRENT_DATE), '%e')
BEGIN
INSERT INTO monthly_scheduling (days) values (day_m)
SET day_m = day_m + 1
END;
$$
DELIMITER ;
The error I'm getting is as follows: MySQL said: #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 '$$ DECLARE day_m INT DEFAULT 0; SET day_m = DATE_FORMAT(CURRENT_DATE, ' at line 1
I've been trying all sorts of things but I can't get the event to create because of the error I seem to keep getting at the DECLARE section. Any help would be much appreciated.