I have this script for update table monthly:
DELIMITER |
CREATE
EVENT `kpiparams_scheduled_update`
ON SCHEDULE
EVERY 1 MONTH
STARTS '2020-02-01 02:59:59'
ON COMPLETION PRESERVE
COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
DO
BEGIN
UPDATE kpiparams INNER JOIN kpiparams_update
ON kpiparams.param_name = kpiparams_update.param_name
SET kpiparams.good = kpiparams_update.good,
kpiparams.bad = kpiparams_update.bad,
kpiparams.weight_gold = kpiparams_update.weight_gold,
kpiparams.weight_tech = kpiparams_update.weight_tech,
kpiparams.is_for_calc = kpiparams_update.is_for_calc
WHERE kpiparams.param_name = kpiparams_update.param_name;
END |
DELIMITER ;
This code drop exception:
Caused by: java.sql.SQLSyntaxErrorException: 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 'DELIMITER | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ST' at line 1
And this:
nested exception is java.sql.SQLSyntaxErrorException: 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 'DELIMITER | CREATE EVENT kpiparams_scheduled_update ON SCHEDULE EVERY 1 MONTH ' at line 1
I serched some resources that said, I need use Delimeter, don't write amount of months in '' and use BEGIN-END. By the way, it didn't help me.
spring.datasource.schema=classpath*:mysql-scripts/event_update_kpiparams.sql
EDITED SCRIPT:
CREATE
EVENT kpiparams_scheduled_update
ON SCHEDULE EVERY 1 MONTH STARTS '2020-02-01 02:59:59'
ON COMPLETION PRESERVE
COMMENT 'KPIParams was updated by event_kpiparams_scheduled_update'
DO
BEGIN
UPDATE kpiparams INNER JOIN kpiparams_update
ON kpiparams.param_name = kpiparams_update.param_name
SET kpiparams.good = kpiparams_update.good,
kpiparams.bad = kpiparams_update.bad,
kpiparams.weight_gold = kpiparams_update.weight_gold,
kpiparams.weight_tech = kpiparams_update.weight_tech,
kpiparams.is_for_calc = kpiparams_update.is_for_calc
WHERE kpiparams.param_name = kpiparams_update.param_name;
END
I guess you tried to run this using JDBC?
You don't need DELIMITER | at all. That's a mysql client builtin command. Client builtins are not recognized by the SQL parser.
You can just execute the CREATE EVENT statement as a single statement and then you don't need to have a delimiter at the end of the statement. Delimiters are only important in interfaces that support multiple statements (e.g. the mysql client).
Okay it seems you are using multiple statements in an .sql file and you need some way of separating the statements. Normally this is ; but you have some statements that contain ; as part of the statement, not as the separator.
I'm not a Spring developer, but I found Spring Boot Database initialization MySQLException for Trigger which describes the use of:
spring.datasource.separator
This is also documented: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html
spring.datasource.separator
(default) ;
Statement separator in SQL initialization scripts.
Related
I am trying to send create procedure query from node js to MYSQL.
I am reading Mysql.proc table to get the stored procedure definitions.
Mysql code working fine when submitting from mysql client like workbench or hedis
Getting following error while submitting code from NodeJS
{ Error: ER_PARSE_ERROR: 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 PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )' at line 1
.
.
.
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlMessage:
'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 PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )\' at line 1',
sqlState: '42000',
index: 0,
sql:
'USE qwe; CREATE PROCEDURE `qwe`.`USP_GET_ALL_STOCK_WITH_INDICATORS`( IN `IPV_DATE` DATE )BEGIN IF NOT EXISTS(SELECT 1 FROM StockCandle where `DATE`=IPV_DATE) THEN SET #D:=(SELECT MAX(`DATE`) FROM StockCandle); ELSE SET #D:=IPV_DATE; END IF; -- select #D; SELECT SM.Symbol, SM.MA13, SM.MA8, SM.MA5, CASE WHEN SM.MA5>SM.MA8 AND SM.MA8>MA13 THEN \'>>>\' WHEN SM.MA5<SM.MA8 AND SM.MA8<MA13 THEN \'<<<\' ELSE NULL
END ALLIGATOR ,SM.RSI, SM.BHAVTIME, SM.CANDLEINDICATOR FROM StockMaster SM where SM.DATE=#D; END // ' }
tried below code to pares sql in node
formattdSQL = formattdSQL.replace(/(?:\\[rtn]|[\r\t]+)+/g, ' ');
Query to get procedure
SELECT `name`, CONVERT(param_list USING utf8), CONVERT(body USING utf8)
INTO #spname, #spparams, #spbody
FROM mysql.proc WHERE `name` = 'USP_GET_ALL_STOCK_WITH_INDICATORS' AND db = v_oldDB;
SET #sql = CONCAT(#sql, '\r\n', 'DELIMITER //','\r\n','CREATE PROCEDURE `', v_newDB, '`.`', #spname, '`(', #spparams,')',#spbody, ' //', '\r\n','');
I expect that create procedure query should be executed successfully from node
The DELIMITER directive is not part of MySQL Server SQL. That is used only with interactive client utilities that parse an input stream or the contents of an on-screen text box into individual statements -- like workbench or (presumably) "hedis" (whatever that may be).
You don't use it when you are using a programming library to send a query.
Just send the procedure declaration as a single query. Send the USE statement as a separate query, before that.
First query:
USE qwe
(A trailing semicolon in the first query is not actually expected by the server but is allowed if you send it.)
Second query:
CREATE PROCEDURE ...
...
END
There should be no DELIMITER // before, nor // after. Those are all client-side constructs, not meaningful to the server.
There is no need to use your formattdSQL.replace(...) statement to remove newlines. That only serves to make your code unreadable, and isn't needed.
I have the following script:
use my_db;
if (2 < 3) then
select 1;
end if;
When I execute this with command:
mysql --user=myuser --password=mypassword < script.sql
I get the following error:
ERROR 1064 (42000) at line 3: 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 'if (2 < 3) then
select 1' at line 1
Can anybody explain me why this?
From mysql docs found here I think it should be working fine.
If you can change your statement, I would recommend it doing it this way:
select if(2<3, 'true','false') as amount
Or wrap your code in a procedure:
create procedure my_procedure()
begin
if (2 < 3) then
select 1;
end if;
end;
-- Execute the procedure
call my_procedure();
-- Drop the procedure
drop procedure my_procedure;
Still don't understand... :). How to check if IF isn't permitted?
https://dev.mysql.com/doc/refman/5.7/en/flow-control-statements.html says:
MySQL supports the IF, CASE, ITERATE, LEAVE LOOP, WHILE, and REPEAT constructs for flow control within stored programs. It also supports RETURN within stored functions.
(emphasis mine)
I wouldn't bother with writing stored routines in MySQL. If you need to do conditional SQL queries, I'd recommend learning a scripting language. Python is a good choice.
#!/bin/env python
import MySQLdb
db = MySQLdb.connect()
if 2 < 3:
cur = db.cursor()
cur.query('select 1')
print cur.fetchall()
What can be the issue in this stored procedure? I have syntax error in this code which I am unable to understand. The sql query inside the code runs perfectly fine but when I try to write it as a stored procedure it throws syntax error.
The error is:
[ERROR in query 1] PROCEDURE 1bot already exists
[ERROR in query 2] 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 'DELIMITER' at line 1
DELIMITER ;;
CREATE PROCEDURE `1bot`()
BEGIN
SELECT
COUNT(distinct orders.customer_id) as Total_Customers,
date_format(orders.created_at,'%M %Y') as month,
COUNT(orders.`increment_id`) as Unique_single_orders,
SUM(orders.`base_subtotal`) as total_rev,
SUM(billing.`total` + 5) as COGS, (
SUM(orders.`base_subtotal`) - SUM(billing.total + 5)) as marketing_expense,
(AVG(orders.`base_subtotal`) - AVG(billing.total + 5)) as avg_acquisition_cost
FROM `sales_flat_order` as orders
LEFT JOIN `sales_flat_order_item` as items on orders.`entity_id` = items.`order_id`
LEFT JOIN `vingo_billing` as billing on orders.`increment_id` = billing.`order_number`
WHERE orders.`created_at` between '2016-09-01 00:00:00' and '2017-01-23'
AND items.sku LIKE "%1bot%"
AND orders.status != "closed"
and orders.shipping_invoiced != '15'
and billing.total > 0
and orders.`total_qty_ordered` < 2;
END ;;
DELIMITER ;
PROCEDURE 1bot already exists
This is self-explanatory. You apparently already have created a procedure named 1bot. MySQL gives you an error because the alternative would be to silently replace the existing procedure with the one you are defining, which may be different. MySQL is protecting you from clobbering a procedure you might need.
If you want to clobber the existing procedure and replace it with new code, you must drop it first:
DROP PROCEDURE `1bot`;;
CREATE PROCEDURE `1bot`() ...
Note that a similar error is given if you try to CREATE TABLE and name a table that already exists.
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 'DELIMITER'
You should know that the DELIMITER statement is only recognized in the mysql command-line client. The purpose is to change the statement delimiter, which is normally ; or \G, because otherwise you couldn't define a stored procedure or trigger with a body that contains multiple semicolon-delimited statements.
Whereas in most other interfaces, e.g. phpMyAdmin, MySQL Workbench, or when running SQL statements in an application, you can only run one statement at a time anyway, so there's no chance of ambiguity. Therefore DELIMITER is not necessary—and not allowed—in those interfaces.
I am trying to make a database that does some inventory checking, and I am working on some triggers to update everything as required. In this case, that I want to do is to take the current availability (disponibilidad) of an ingredient from the ingredients table, add to it how much is taken or added, and then save that value into the table stock_disponibilidad, as you can see from the trigger code I am trying to use.
DELIMITER $$
CREATE TRIGGER `update_disponibilidad_variacion` BEFORE INSERT ON `stock_disponibilidad`
FOR EACH ROW BEGIN
DECLARE old_disp DOUBLE DEFAULT 0;
SELECT disponibilidad INTO old_disp FROM ingredientes WHERE id = NEW.ingredientes_id;
NEW.disponibilidad = old_disp + NEW.variacion;
END$$
DELIMITER ;
However, whenever I execute the query to create the trigger, I get the following error:
#1064 - 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 '.disponibilidad = old_disp + NEW.variacion; END' at line 5
I've done everything I read about this issue, still without result. What am I doing wrong?
OK something weird is going on and I can't figure it out. I'm creating some triggers, but I get strange errors when trying to create them on the live DB. A sample query is this:
delimiter |
CREATE TRIGGER debug_upd_before BEFORE UPDATE ON properties_availability
FOR EACH ROW BEGIN
INSERT INTO debug_upd_before SET timestamp = NOW(), avID = OLD.avID, avPropertyID = OLD.avPropertyID,
avAvailableFrom = OLD.avAvailableFrom, avAvailableTo = OLD.avAvailableTo, avPrice = OLD.avPrice,
avIsAvailable = OLD.avIsAvailable;
END|
...
which executes fine localy. I'm using phpmyadmin, cause I don't have any other access to the online DB. I know I don't have full permissions and think that it might have something to do with that, but the error I get 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 'delimiter |
not "you don't have permissions to do that" or something like that. The same happens when I try to execute something simpler like:
delimiter |
select 1|
select 2
The error I get is the same and I can't set a delimiter and the default doesn't work for me cause I have to use after the queries inside the triggers. I tried other symbols as delimiters, but the result is the same. Any ideas as to what the problem is and how to avoid it? Some of the triggers execute more than one query, if that makes a difference...
Thanks!
Try this statement (without delimiters) -
CREATE TRIGGER debug_upd_before BEFORE UPDATE ON properties_availability
FOR EACH ROW
INSERT INTO debug_upd_before SET
timestamp = now(),
avID = OLD.avID,
avPropertyID = OLD.avPropertyID,
avAvailableFrom = OLD.avAvailableFrom,
avAvailableTo = OLD.avAvailableTo,
avPrice = OLD.avPrice,
avIsAvailable = OLD.avIsAvailable;
The problem is that | is a bitwise or.
So you're using a reserved symbol as a delimiter.
Use $$ instead.