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.
Related
I am attempting to recreate a stored procedure (since I can't edit the body). I called SHOW CREATE PROCEDURE to use the same format as the original stored procedure but when I attempt to recreate it I get the following errors:
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 '' at line 11
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 'DECLARE organization_id BIGINT(20) UNSIGNED' at line 1
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 'DECLARE lobby_pod_id BIGINT(20) UNSIGNED' at line 1
Here's the code:
CREATE DEFINER=`lms`#`10.0.0.%` PROCEDURE `create_organization`(
IN admin_username VARCHAR(255),
IN organization_name VARCHAR(100)
)
BEGIN
DECLARE admin_user_id BIGINT(20) UNSIGNED;
DECLARE organization_id BIGINT(20) UNSIGNED;
DECLARE lobby_pod_id BIGINT(20) UNSIGNED;
SELECT ID, account INTO admin_user_id, organization_id
FROM users
WHERE username = admin_username;
INSERT INTO pods (`title`, `description`, `owner`, `scene`)
VALUES (CONCAT(organization_name, " Village"),
CONCAT("General meeting space and hub for ", organization_name, " students and teachers."),
admin_user_id,
" Village"
);
END
I pasted into SQL Fiddle and got the same result, although pasting into MySQL Syntax Check gave me the thumbs-up. I'm sure it's a simple miss but it isn't that obvious to me.
You are missing the delimiter definition before and after the stored proc definition:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. [...] The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Since the stored proc definition and body was ok, syntax chack gave you the thumbs up, but the code would not run properly in your client.
Use the following skeleton for defining a stored procedure:
delimiter //
create procedure ...
...
end
//
delimiter ;
I am getting an error in my mySql query when i execute my create trigger query in mysql using phpmyadmin which is provided below.
Error:
Error Message :
#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 '' at line 5
MySql Query:
CREATE TRIGGER Trigger_INSERT_club_app_member_contact_info After INSERT ON club_app_member_contact_info
FOR EACH ROW
BEGIN
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(1,CONCAT('Happy Birthday ',NEW.title,NEW.name),NEW.mobile_no,NEW.dob,'8.30AM','');
IF NEW.ismarried == 'Yes' THEN
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(1,CONCAT('Happy Birthday ',NEW.title_for_spouse,NEW.nameOf_spouse,'Spouse of (',NEW.title,NEW.name,')'),NEW.spouse_mobileNo,NEW.spouse_dob,'8.30AM','');
INSERT INTO club_app_events_list(type_id,event_title,event_description,event_date,event_time,event_venue)
values(2,CONCAT('Happy Wedding Anniversary to ',NEW.title,NEW.name ,' & ',NEW.title_for_spouse,NEW.nameOf_spouse),NEW.mobile_no,NEW.weeding_date,'8.30AM','');
END IF
END
I don't know what's wrong with my query.Can anyone help me to find a solution.
Few thing i noticed IF NEW.ismarried == 'Yes' THEN it should IF NEW.ismarried = 'Yes' THEN BEGIN and END IF should end with semicolon ; although BEGIN end enclose END;
But since i am trying to execute this query using phpmyadmin I have to remove Delimiter from the bottom text box of phpmyadmin.
But note that this is only phpmyadmin analysis warnings not a mysql server response.
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'm experimenting triggers functions in mysql. I got error as You have a sql syntax error at line 3. The code which I used,
use maas;
create trigger dummy_trigger after insert on dummy
for each row begin
declare cmd varchar(255);
declare result integer(10);
set cmd = concat('python /home/yogaraj/for_sql.py');
set result = sys_exec(cmd);
end;
Error Output:
Error Code: 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 '' at line 3
Aim: I'm trying to execute a python file located in the home directory. I'm using mysql workbench.
If you use an SQL editor for this code then you have to specify a delimiter so that the inner commands of the create trigger code don't get parsed as individual statements.
However you can use the trigger pane in the table editor to create a trigger without all the decoration:
The blue dot left in the gutter shows that your statement is perfectly ok.
So I have this stored proc that will not get created when I run the file.
DELIMITER //
DROP PROCEDURE IF EXISTS msd.test_proc//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM
msd.zipcode_lookup;
END//
DELIMITER ;
When I run this I get an error code 1064 at line 1 when I execute in RazorSQL. Here is the complete error message:
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 msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM ' at line 1
Error Code:1064
I've tried other variations and still get errors. I am sure this is something basic I am missing. I appreciate any help.
Thanks.
As stated on the RazorSQL website:
The DELIMITER statement is not part of the MySQL language. It is a command supported by certain MySQL tools. This command tells those MySQL programs to scan for a certain character that indicates the end of a query or statement.
RazorSQL does not support using the DELIMITER command. The SQL statement delimiter value used by RazorSQL can be changed using the preferences window. The default values is the semi-colon.