Trouble with basic mySql procedure - mysql

mysql> show tables;
+---------------------+
| Tables_in_cpsc408db |
+---------------------+
| Product |
| laptop |
| pc |
| printer |
+---------------------+
4 rows in set (0.00 sec)
mysql> create procedure hello()
-> begin
-> select * from product;
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
mysql>
I am not sure what is causing this syntax error, and haven't had any success so far figuring it out. Any help would be much appreciated.

You need to use the DELIMITER keyword:
This is so MySQL can tell which statements are within the procedure and where the end of the procedure declaration itself is
delimiter //
CREATE PROCEDURE hello()
BEGIN
select * from product;
END//
delimiter ;

You need a delimiter otherwise the console does not know when you are finished:
DELIMITER //
CREATE PROCEDURE hello()
BEGIN
SELECT * FROM product;
END //
DELIMITER ;
Read more about it here: Getting Started with MySQL Stored Procedures

Related

Mysql_ubuntu_ERROR 1064 (42000)

mysql> delimiter &&
mysql> create procedure get()
-> begin
-> select * from a;
-> end&&
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 'get()
begin
select * from a;
end' at line 1
mysql> show tables;
+----------------+
| Tables_in_data |
+----------------+
| a |
| test |
+----------------+
This is because GET is a reserved word in MySql, and therefore cannot be used as an object name without using backticks.
The following will work:
delimiter &&
create procedure `get`()
begin
select * from a;
end&&
delimiter ;
Alternatively, use another name for the procedure (which would be my recommendation as using reserved words as object names is unwise).

Mariadb: assignment operator (:=) not working with local variables

The Mariadb Knowledge base explains that the assignment operator (:=) works with both user-defined variables and local variables.
But when I make some tests, for example :
delimiter //
CREATE or replace PROCEDURE tst_PS_now()
BEGIN
declare varnow datetime(3);
set varnow = (select now());
select 'result: ' ,varnow;
END;
//
delimiter ;
mysql:root> call tst_PS_now() ;
+----------+-------------------------+
| result: | varnow |
+----------+-------------------------+
| result: | 2020-10-26 16:13:44.000 |
+----------+-------------------------+
example 2:
delimiter //
CREATE or replace PROCEDURE tst_PS_now()
BEGIN
declare varnow datetime(3);
select varnow:=now();
select 'result: ' ,varnow;
END;
//
delimiter ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':=now(); at line 4
In the second example, the assignment should work with my local variable.
I really need this kind of assignment in my stored procedures.
How may I fix it ? Any idea?

MySQL create stored procedure syntax with delimiter

I am trying to create a stored procedure in MySQL using a delimiter like this:
use am;
DELIMITER $$
CREATE PROCEDURE addfields()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE acc INT(16);
DECLARE validId INT DEFAULT 0;
END $$
DELIMITER ;
It gives me an error:
#1304 - PROCEDURE addfields already exists
What is the proper syntax for making a stored procedure with a delimiter and dropping it if it exists first?
Getting started with stored procedure syntax in MySQL (using the terminal):
1. Open a terminal and login to mysql like this:
el#apollo:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
2. Take a look to see if you have any procedures:
mysql> show procedure status;
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yourdb | sp_user_login | PROCEDURE | root#% | 2013-12-06 14:10:25 | 2013-12-06 14:10:25 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.01 sec)
I have one defined, you probably have none to start out.
3. Change to the database, delete it.
mysql> use yourdb;
Database changed
mysql> drop procedure if exists sp_user_login;
Query OK, 0 rows affected (0.01 sec)
mysql> show procedure status;
Empty set (0.00 sec)
4. Ok so now I have no stored procedures defined. Make the simplest one:
mysql> delimiter //
mysql> create procedure foobar()
-> begin select 'hello'; end//
Query OK, 0 rows affected (0.00 sec)
The // will communicate to the terminal when you are done entering commands for the stored procedure. the stored procedure name is foobar. it takes no parameters and should return "hello".
5. See if it's there, remember to set back your delimiter!:
mysql> show procedure status;
->
->
Gotcha! Why didn't this work? You set the delimiter to // remember? Set it back to ;
6. Set the delimiter back and look at the procedure:
mysql> delimiter ;
mysql> show procedure status;
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yourdb | foobar | PROCEDURE | root#localhost | 2013-12-06 14:27:23 | 2013-12-06 14:27:23 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
7. Run it:
mysql> call foobar();
+-------+
| hello |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Hello world complete, lets overwrite it with something better.
8. Drop foobar, redefine it to accept a parameter, and re run it:
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> show procedure status;
Empty set (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (in var1 int)
-> begin select var1 + 2 as result;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(5);
+--------+
| result |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Nice! We made a procedure that takes input, modifies it, and does output. Now lets do an out variable.
9. Remove foobar, Make an out variable, run it:
mysql> delimiter ;
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar(out var1 varchar(100))
-> begin set var1="kowalski, what's the status of the nuclear reactor?";
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(#kowalski_status);
Query OK, 0 rows affected (0.00 sec)
mysql> select #kowalski_status;
+-----------------------------------------------------+
| #kowalski_status |
+-----------------------------------------------------+
| kowalski, what's the status of the nuclear reactor? |
+-----------------------------------------------------+
1 row in set (0.00 sec)
10. Example of INOUT usage in MySQL:
mysql> select 'ricksays' into #msg;
Query OK, 1 row affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (inout msg varchar(100))
-> begin
-> set msg = concat(#msg, " never gonna let you down");
-> end//
mysql> delimiter ;
mysql> call foobar(#msg);
Query OK, 0 rows affected (0.00 sec)
mysql> select #msg;
+-----------------------------------+
| #msg |
+-----------------------------------+
| ricksays never gonna let you down |
+-----------------------------------+
1 row in set (0.00 sec)
Ok it worked, it joined the strings together. So you defined a variable msg, passed in that variable into stored procedure called foobar, and #msg was written to by foobar.
Now you know how to make stored procedures with delimiters. Continue this tutorial here, start in on variables within stored procedures: http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/
Here is the sample MYSQL Stored Procedure with delimiter and how to call..
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_user_login` $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_user_login`(
IN loc_username VARCHAR(255),
IN loc_password VARCHAR(255)
)
BEGIN
SELECT user_id,
user_name,
user_emailid,
user_profileimage,
last_update
FROM tbl_user
WHERE user_name = loc_username
AND password = loc_password
AND status = 1;
END $$
DELIMITER ;
and call by, mysql_connection specification and
$loginCheck="call sp_user_login('".$username."','".$password."');";
it will return the result from the procedure.
Here is my code to create procedure in MySQL :
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `procedureName`(IN comId int)
BEGIN
select * from tableName
(add joins OR sub query as per your requirement)
Where (where condition here)
END $$
DELIMITER ;
To call this procedure use this query :
call procedureName(); // without parameter
call procedureName(id,pid); // with parameter
Detail :
1) DEFINER : root is the user name and change it as per your username of mysql localhost is the host you can change it with ip address of the server if you are execute this query on hosting server.
Read here for more detail
I have created a simple MySQL procedure as given below:
DELIMITER //
CREATE PROCEDURE GetAllListings()
BEGIN
SELECT nid, type, title FROM node where type = 'lms_listing' order by nid desc;
END //
DELIMITER;
Kindly follow this. After the procedure created, you can see the same and execute it.
MY SQL STORED PROCEDURE CREATION
DELIMiTER $$
create procedure GetUserRolesEnabled(in UserId int)
Begin
select * from users
where id=UserId ;
END $$
DELIMITER ;

MySQL Syntax error

I am unable to run a simple script in MySQL. I have reduced the script to just one line.
DELIMITER $$
DECLARE varLocalityName VARCHAR(50);
$$
DELIMITER ;
The error is:
ERROR 1064 (42000) at line 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 'DECLARE varLocalityName VARCHAR(50)' at line 1
$ mysql --version
mysql Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.2
Your code block does not define the scope for the declared variables. If within a procedure, they must be between BEGIN and END. Without them, the statement DECLARE varLocalityName VARCHAR(50); becomes an invalid statement to be executed. This statement is equivalent to the statements shown below:
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2012-10-22 |
+----------------+
1 row in set (0.00 sec)
mysql> declare varLocalityName varchar(50);
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 varLocalityName varchar(50)' at line 1
mysql>
Either you should declare session variables without keyword DECLARE or follow a syntax defined for a stored procedure to use scoped variables.
Example 1: Using session variables:
mysql> set #x = null;
Query OK, 0 rows affected (0.00 sec)
mysql> select #x;
+------+
| #x |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> delimiter $$
mysql> select current_date() into #x;
-> $$
Query OK, 1 row affected (0.02 sec)
mysql> select #x;
-> $$
+------------+
| #x |
+------------+
| 2012-10-22 |
+------------+
1 row in set (0.00 sec)
Note that you can set/define session variables within a procedure but not DECLARE.
Example 2: Using procedure scoped variables:
mysql>
mysql> delimiter $$
mysql> create procedure some_x()
-> begin
-> declare varLocalityName varchar(50);
->
-> set #sessionDate = null;
-> select #sessionDate;
-> set #sessionDate = current_date();
-> select #sessionDate;
->
-> select varLocalityName;
-> end;
-> $$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql>
mysql> call some_x();
+--------------+
| #sessionDate |
+--------------+
| NULL |
+--------------+
1 row in set (0.00 sec)
+--------------+
| #sessionDate |
+--------------+
| 2012-10-22 |
+--------------+
1 row in set (0.00 sec)
+-----------------+
| varLocalityName |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> select #sessionDate;
+--------------+
| #sessionDate |
+--------------+
| 2012-10-22 |
+--------------+
1 row in set (0.00 sec)
mysql> select varLocalityName;
ERROR 1054 (42S22): Unknown column 'varLocalityName' in 'field list'
mysql>
Also refer to Variables declaration and scope.
I think the problem is that you define $$ as delimiter and then you still try to use ; as the delimiter
I believe this should work:
DELIMITER $$
DECLARE varLocalityName VARCHAR(50)$$
DELIMITER ;
As default MySQL delimiter is ;. You can change it with DELIMITER call and you must use it in the new syntax from then on.
DELIMITER $$
DECLARE varLocalityName VARCHAR(50)$$
SET varLocalityName="xx"$$
SELECT varLocalityName$$
DELIMITER ;
So basically $$ replaces every occurrence of ; (as long as it is not inside the String). If you set DELIMITER **, you would use double stars instead of ;.

Mysql 5.0 Stored Procedure Syntax

I'm just trying to create my first mysql stored procedure and I'm trying to copy some examples almost directly from the documentation, but it isn't working:
mysql> delimiter //
mysql> CREATE PROCEDURE ghost.test (OUT param1 INT) INSERT into admins SELECT COUNT(*) FROM bans; END//
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
What is the deal here? This is almost identical to:
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END//
Query OK, 0 rows affected (0.00 sec)
From
http://dev.mysql.com/doc/refman/5.1/en/create-procedure.html
Looks like you're missed the BEGIN.