Stored Procedure MySQL Syntax error in Adminer - mysql

I'm very new to stored procedures in MySQL in general. I am trying to create one in Adminer and I keep getting a syntax error message:
Syntax error near '$$ CREATE PROCEDURE test() BEGIN SELECT * from lead; END$$ DELIMITER' at line 2
I'm trying to create a procedure named test to select all the records from a table. The code I put into Adminer is as follows:
DELIMITER $$
CREATE PROCEDURE test()
BEGIN
SELECT * from lead;
END$$
DELIMITER ;
However, if use the MySQL CLI, and enter the exact same code line for line, it works
mysql> DELIMITER $$
mysql> CREATE PROCEDURE test()
-> BEGIN
-> SELECT * from lead;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
If I call the procedure test, I get all the records from the lead table.
I've logged into both Adminer and the CLI with the same credentials so I'm fairly confident it's not a permissions thing. I know I am missing something; can any one assist with a point in the right direction?

Related

Procedure run time

DELIMITER $
DROP PROCEDURE IF EXISTS CREATE_BACKUP$
CREATE PROCEDURE CREATE_BACKUP()
BEGIN
DECLARE BACK INT DEFAULT 0;
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'STUDENTDB'
;
SHOW_LOOP:LOOP
IF BACK = 1
THEN
LEAVE SHOW_LOOP;
END IF;
CREATE TABLE STUDENT_BACKUP
AS SELECT * FROM STUDENT;
CREATE TABLE SCORE_BACKUP
AS SELECT * FROM SCORE;
CREATE TABLE GRADE_EVENT_BACKUP
AS SELECT * FROM grade_event;
END LOOP SHOW_LOOP;
END$
DELIMITER ;
Hi, when I run this procedure, it runs more than one time. So I get an error which says "STUDENT_BACKUP table already exists" for the second time when it runs. What should I do to run it just 1 time?
In MySQL you can use CREATE TABLE IF NOT EXIST... to avoid the error occurrence. See CREATE TABLE syntax for details.
To solve your quesrion for SQL server use an INFORMATION_SCHEMA view. A similar solution is in the existing topic.

Pretty printing not working for stored proc in MySQL

I am trying to write an simple stored procedure to just print out information from three different tables. The problem is that for some reason \G does not work from within a stored proc. I want the output to be readable so this is pretty important
This code works but doesnt display columns in an effective way
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername;
END //
DELIMITER ;
This code throws an exception
DELIMITER //
DROP PROCEDURE IF EXISTS `snapshot`//
CREATE PROCEDURE snapshot(IN employeeUsername varchar(255))
BEGIN
SELECT * FROM employee where username = employeeUsername \G;
END //
DELIMITER ;
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 3
Any help would be greatly appreciated
\G is an command in MySQL Client and subsequently can not be called from a sql stored proc

MySql Stored Procedure mangles str_to_date() function

MySql Stored Procedure mangles str_to_date() function.
When the following MySQL query is run in the terminal mode it works fine.
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
Query OK, 75 rows affected, 2028 warnings (0.01 sec)
Rows matched: 2028 Changed: 75 Warnings: 0
However, when placed within a MySQL Stored Procedure the update query is mangled.
DELIMITER $$
CREATE PROCEDURE test_upd
BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
End $$
DELIMITER ;
Please Note str_to_date() function has changed thus gives an 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'BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/'
at line 2
Is there a work-a-round for this problem ?
Thanks.
Edward.
You need a () along with your stored procedure name. Your procedure body should look like below. Refer MySQL Documentation On Create procedure for more information.
DELIMITER $$
CREATE PROCEDURE test_upd()
BEGIN
UPDATE `tt002grawdata` SET `bizdate` = str_to_date(`BusinessDate`,'%m/%d/%y');
End $$
DELIMITER ;

Execute triggers stored procedures on SqlFiddle. Mysql

Does SQL-fiddle facilitate execution of triggers/stored procedures?
I have been unable to execute even the simplest form of stored procedure on sqlfiddle
DELIMITER $$
DROP PROCEDURE IF EXISTS myProc $$
CREATE PROCEDURE myProc()
BEGIN
END$$
DELIMITER ;
Sqlfiddle does not allow executing this(above) sql in build schema, but allows create table etc
Note: The same syntax is working for me on my localhost using wamp with mysql 5.5.24
Can anyone guide please?
Instead of using the delimiter option (which is not a real SQL statement, but rather only a command for the mysql command prompt) use the "Query Terminator" option on SQL Fiddle to establish your delimiter.
For example:
http://sqlfiddle.com/#!2/88fcf
Note the // dropdown below the schema box? That's the SQL Fiddle equivalent to the mysql DELIMITER command.
Longer example with queries in the stored procedure (note that within the stored procedure, ; is still used as a delimiter):
http://sqlfiddle.com/#!9/4db78
Full disclosure: I'm the author of SQL Fiddle.
I couldn't get this answer to work on sql fiddle, but found db-fiddle, and it seems to work.
Example in DB Fiddle
If the above doesn't work for some reason, do the following
Go here: https://www.db-fiddle.com/
Enter the following SQL on the left, and SELECT * FROM tblTest; on the right.
Select "MySql 5.7" or whatever in dropdown.
Click "Run"
DELIMITER //
CREATE TABLE tblTest (col1 INT)//
INSERT INTO tblTest VALUES (9)//
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 3;
WHILE v1 > 0 DO
INSERT INTO tblTest VALUES(v1);
SET v1 = v1 - 1;
END WHILE;
END//
INSERT INTO tblTest VALUES (8)//
select * from tblTest//
call dowhile()//
select * from tblTest//
DELIMITER ;

Calling a user defined stored procedure from select statement mysql

I am trying to call a user defined stored procedure from a select statement and its giving me an error. However when I call a system procedure it works just fine. Is there a way to call a user defined procedure from a select statement. This is for mysql
SELECT ID, email FROM user PROCEDURE simpleproc();
give me an error ERROR 1106 (42000): Unknown procedure 'simpleproc'
mysql> call simpleproc();
Query OK, 0 rows affected (0.21 sec)
where as
SELECT ID, email FROM user PROCEDURE ANALYSE();
works
You can call stored procedure from select statement,To call a procedure you must use following syntax:
CALL stored_procedure_name (param1, param2, ....)
Such as, you can CALL following procedure:
DELIMITER //
CREATE PROCEDURE `procedure1`(IN var1 INT)
BEGIN
SELECT var1 + 2 AS result;
END//
as
CALL procedure1(10);
Check this site for reference: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/