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()
Related
I need to create a trigger in sql server via R code for which I need to set my sql delimiter to //.
I tried doing the following:
dbExecute(con, "delimiter //")
dbExecute(con, "delimiter //\n")
dbExecute(con, "delimiter //\t")
I also tried the above scenarios with other DBI functions like
dbGetQuery and dbSendQuery
but I am getting the following error.
could not run statement: 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
It turns out that in order to execute an sql trigger through R using the DBI package, one does not need to set and unset the delimiter. We can directly execute the trigger command.
This is unlike what needs to be done while setting a triggers through SQL command line where, since the trigger syntax itself includes a semicolon ;, in order to avoid conflict with the default SQL delimiter which is also ; we temporarily set the delimiter to a lesser used special character such as // with a command such as
delimiter //
and then revert back to the default delimiter with
delimiter ;
which need not be done when trigger is executed through DBI package of R.
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.
Ok so, I've been ripping my hairs ou on this one, why doesn't this work?
DELIMITER |
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'Hey Select';
END|
It's so basic and I'm pretty sure I'm using the correct syntax, what am I missing?
Error:
21:14:07 [DELIMITER - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 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 'DELIMITER |
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'He' at line 1
21:14:07 [END| - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 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 'END|' at line 1
I'm using DbVisualizer, latest version, could this problem be with the software itself?
Perhaps I should explain myself better, the passwords are encrypted in my database (no need to worry), and this allows me to decrypt them, this is for a personal project I'm working on.
I was trying to develop a script that would allow me to run it and set up the necessary databases, tables, etc for that to work, and I require some SPs which must also be created, I'm trying to create an SP through a mysqli_query, is that even possible?
Basically it's for a "setup script" of a php application.
UPDATE: Seems that this is supposed to work, however I can't use objects due to the guys at HostGator -.- not allowing for objects in PHP.
I Have pretty much given up on mysqli since it's just not going to work I'm trying with shell_exec, I'm creating the procedure but when I check the ddl it's empty, it's creating empty procedures but at least it's doing something...
it is probaly a software version problem... i tried your code and it works just fine for me...
try this
DELIMITER //
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'Hey Select';
END //
DELIMITER ;
At least as of 9.1, DBVisualizer doesn't support the DELIMITER keyword. Here's the way they do it: link.
Definitely Not an elegant work-around ... but it works.
All the usual caveats about not shelling out, yada yada yada.
// here's the core stored procedure code
$stored = <<<EOT
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT * FROM whatever;
END #
EOT;
// first, shell out to change the delimiter using mysql command-line
shell_exec('mysql -u user -ppassword -e "DELIMITER #");
// assuming $pdo is a valid PDO connection
// send the command to create the stored procedure:
$pdo->exec($stored);
// now shell out again to change the delimiter back
shell_exec('mysql -u user -ppassword -e "DELIMITER ;");
Try putting space between 'DELIMITER' and '|'.
It worked for me.
DELIMITER | --here
CREATE TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
INSERT INTO trigger_test VALUES('added new employee');
END |
DELIMITER;
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.
I have a very simple trigger. I cretaed it from Toad for mysql tool and deployed it and its working perfect without any problem. when I gave it to the admin to deploy in the production server they are getting errors.
Phpmyadmin 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 '' at line 4
Mysql console error:
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 'END' at line 1
When asked, they are running the code from phpmyadmin and also they tried to run from mysql console they are getting errors
Its really frustrating and irritating why the same thing is running from a GUI tool and not working from a webtool or a console. 3 different behaviors in from three different tools
Then I tried the same thing and I got the error too. Its suprising me why
DROP TRIGGER IF EXISTS TRG_ ;
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) into X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END;
Can some one tell me what I am doing wrong? I am not sure but I guess its something to do with delimiter keyword which I never understood the purpose.
Have a look at this question - MySQL: How do I use delimiters in triggers?
You should use delimiters when create source objects like triggers. The DELIMITER is not a MySQL statement, it a console command, and many MySQL clients supports this command. You may try this code in the MySQL console -
DROP TRIGGER IF EXISTS TRG_ ;
DELIMITER $$
CREATE TRIGGER TRG_ BEFORE INSERT ON users FOR EACH ROW
BEGIN
DECLARE X INTEGER;
SELECT COUNT(*) INTO X FROM users;
IF X >= 16 THEN -- CHANGE THIS NUMBER
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'cant create more users';
END IF;
END$$
DELIMITER ;
As I know some old phpmyadmin versions do not support delimiters.