MySQL stored proc not getting created - mysql

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.

Related

How do I set sql delimiter through R code?

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.

#1064 Error SQL - Happens on all my procedures/functions

I know this question has been asked a bunch in different forms, but none of the ones I looked at (quite a bit) seemed to help me out in my specific case. I wrote a few functions and procedures and I always get the same error at the same spot. Here is my code:
DELIMITER |
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId, Name FROM STUDENT WHERE Status = sts;
END |
DELIMITER;
This happens on all my procedures functions, this 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 'CREATE PROCEDURE STUDENTS_BY_STATUS (sts VARCHAR(10))
BEGIN
SELECT BannerId,' at line 2
On my other one its this:
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 'CREATE FUNCTION GoodGrade(letGrade VARCHAR(2)) RETURNS int
BEGIN
DECLARE v' at line 2
This happens whether or not I use | or // as the DELIMITER...can someone tell me what I'm doing wrong here? Thanks!
You've changed the delimiter, written a statement and then not delimited it according to your new definition
Change the line
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS;
to
DROP PROCEDURE IF EXISTS STUDENTS_BY_STATUS |
Update
This wasn't obvious from the initial error message, but you have another error when you reset the Delimiter after the procedure definition. You need a space before the ';' on that one.
so change
DELIMITER;
to
DELIMITER ;
NB
The other use of ';' within your procedure definition is correct, because you want everything within the create statement to to be processed together.

Error near 'DELIMITER $$'

when I change Delimeter from mysql console or MySQL Workbench I do not get any error,
but when I embed the same code in ruby on rails I get error
mysql> DELIMITER $$
mysql>
gives no error.
but
ActiveRecord::Base.connection.execute(%Q{
DELIMITER $$
})
gives:
ActiveRecord::StatementInvalid: Mysql2::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 'DELIMITER $$' at line 1:
The top answer is correct (Rails cannot execute DELIMITER because it is a MYSQL command), but #ishandutta2007's follow up question was not answered, so I'll answer that here.
DELIMITER is often used to wrap mysql function and procedure bodies; to achieve this in rails simply wrap the procedure body in it's own execute statement.
So for instance code that might read like:
execute <<-SQL
DROP FUNCTION IF EXISTS MyFunc;
DELIMITER $$
CREATE FUNCTION My Func
. . .
$$
DELIMITER ;
SQL
Would instead become the following, with the multiple execute calls acting as the 'scoping' intended by redefining the delimiter:
execute 'DROP FUNCTION IF EXISTS MyFunc'
execute <<-SQL
CREATE FUNCTION My Func
. . .
SQL
DELIMITER is actually a MySQL command line setting, not SQL: http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html. That means that you can't set the delimiter in this way.
On top of that, it wouldn't help if you could as ActiveRecord::Base.connection.execute only allows you to execute one statement at a time out of the box (see http://www.seanr.ca/tech/?p=75).

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 !!!

The MySQL "DELIMITER" keyword isn't working

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;