The MySQL "DELIMITER" keyword isn't working - mysql

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;

Related

How can I get my Trigger to work in MySQL 8.0?

The following trigger works perfectly well in MySQL 5.7. I have used it extensively.
DELIMITER $$
#
# FUNCTIONS
#
-- #_path
DROP FUNCTION IF EXISTS `path`$$
CREATE DEFINER=CURRENT_USER FUNCTION path(id INT unsigned, level BOOLEAN) RETURNS VARCHAR(3000)
DETERMINISTIC
BEGIN
...
END $$
#
# TRIGGERS
#
-- #_root - INSERT MATERIALIZED PATH & RANK
DROP TRIGGER IF EXISTS tx_tt_domain_model_root_Bi_0;
CREATE TRIGGER tx_tt_domain_model_root_Bi_0 BEFORE INSERT ON tx_tt_domain_model_root
FOR EACH ROW
BEGIN
SET #_relation=if(NEW.relation,path(NEW.relation,false),null);
SET #_role=if(NEW.role,path(NEW.role,false),null);
...
END $$
...
DELIMITER ;
After upgrading to MySQL 8.0, the trigger throws the following error when attempting to load it:
[2022-06-24 13:36:05] [42000][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 '(NEW.relation,false),null);
[2022-06-24 13:36:05] SET #_role=if(NEW.role,path(NEW.role,false),null)' at line 8
EDIT; I created a fresh install of MySQL 8.0 and loaded it with all the necessary tables. Am now loading stored procedures and triggers and getting stuck while loading the trigger above.
Perhaps am missing something in plain sight or haven't quite understood how MySQL 8 really works or something else. What am I doing wrong? Please help.
A big thank you to Akina for pointing out that path is actually a non-reserved keyword introduce in MySQL 8.0.4.
Just renamed the custom function to create_path and now loads without complaining.

Does --init-file parameter support the creation of procedures through a .sql file?

So I have this SQL file whose contents are like below
DELIMITER $$
CREATE PROCEDURE FOO(....)
BEGIN
Insert into BAR(...) Values(....);
Insert into BARTOO(...) Values(...);
END$$
DELIMITER ;
Now this seems to work just fine when we I use the mysql client to execute this script. However If i pass it to initialize as a --init-file=./myscript.sql this fails with the follow error
2020-06-04T04:22:37.307204Z 5 [ERROR] [MY-000061] [Server] 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 'DELIMITER
$$ CREATE PROCEDURE FOO(..... at line 1.
Initially this made sense that the keyword DELIMITER is not supported by the SQL syntax and that this is purely a client related command.Does that mean you cannot create a multi-line stored procedure using an --init-file? is there another way to create this procedure on initialization?
I also came across this bug report (https://bugs.mysql.com/bug.php?id=17843) that seems to indicate that DELMITER is supported in --init-file??
very confused, please help.
I don't known about mysql but there's an open bug on mariadb : https://jira.mariadb.org/browse/MDEV-18394
The dev team seems to say that this is the same behavior as mysql. They are probably not going to fix this soon as this is marked as a feature request.
I don't know about the context in which you are using this init script, but in my case this is for a container initialization, which makes it hard to use the mysql client. I ended up using a small shell script as my entry point, starting the mysqld in the background with a minimal init file, waiting a little bit, then passing the commands with the mysql client, then putting the mysqld back to foreground.
Single-line procedures do appear to work in init files:
init.sql
USE mysql;
CREATE OR REPLACE DEFINER='root'#'localhost' PROCEDURE abc() READS SQL DATA BEGIN SELECT 1; SELECT 2; SELECT 3; END;

IF in MySQL script

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()

How to copy MySQL function?

I have tried to use the export option in phpMyAdmin routines panel to copy functions from one database to another, with no success.
The export option supplies me with the following:
CREATE DEFINER=`root`#`localhost` FUNCTION `JSON_FIELD_NUM`(`col_name` TEXT CHARSET utf8, `data` TEXT CHARSET utf8) RETURNS text CHARSET utf8
NO SQL
BEGIN
RETURN
CONCAT('"',col_name,'":',
IF(ISNULL(data),0,data)
);
END
I get this error when I run that in another database:
#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 7
I tried adding DELIMITER $$ at the top and $$ after END, but still no joy.
You must set your client's statement delimiter to a string other than ; in order that it doesn't think the semicolon which ends the CONCAT() expression also terminates the CREATE FUNCTION statement.
In the MySQL command line tool, you can use the DELIMITER command. In phpMyAdmin, you will need to use the Delimiter text box before clicking Go.
There is a far more concise way to do this:
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --routines"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --all-databases"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --no-data"
MYSQLDUMP_OPTIONS="${MYSQLDUMP_OPTIONS} --no-create-info"
mysqldump ${MYSQLDUMP_OPTIONS} > StoredProcedures.sql
less StoredProcedures.sql
This will dump only Stored Procedures.
Give it a Try !!!

MySQL stored proc not getting created

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.