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 ;.
Related
DELIMITER $
DROP PROCEDURE IF EXISTS GET_AVG$
CREATE PROCEDURE GET_AVG()
BEGIN
DECLARE l_avg decimal;
DECLARE iam_done int DEFAULT 0;
DECLARE avg_cursor CURSOR FOR
SELECT AVG(score)
FROM score
GROUP BY student_id
;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET iam_done=1;
BEGIN
OPEN avg_cursor;
tbl_loop:LOOP
FETCH avg_cursor INTO l_avg;
IF iam_done = 1 THEN
leave tbl_loop;
END IF;
SELECT l_avg;
END LOOP tbl_loop;
CLOSE avg_cursor;
END;
END$
DELIMITER ;
++++++++++++++++++++++++++++++++++++++++++
My output is not giving me the right values:
mysql> call get_avg();
+----------------+
| #l_avg |
+----------------+
| 0x |
+----------------+
1 row in set (0.00 sec)
+----------------+
| #l_avg |
+----------------+
| 0x |
+----------------+
1 row in set (0.00 sec) ...
++++++++++++++++++++++++++++++++++++++++++
Any idea why I am having a "0x" as returned value?
I'm quite new with MySQL. I have to call a stored procedure with output param. I've searched a lot on internet but I've not found the correct solution to my problem. If I call the stored procedure with the #outputParamName it says that I have an error #1064 near NULL. If I call the procedure with the 'outputParamName' without the # it says thath it is not an OUT or INOUT correct param. Someone can help me please?
the stored procedure just have to check if surname and name in DB exists on the same row:
CREATE PROCEDURE InsertProc (INOUT existsInDb BOOLEAN,
IN dbName VARCHAR(50)
IN tableName VARCHAR(50)
IN surnameNew VARCHAR(50)
IN nameNew VARCHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE rowSurnameName int;
SET #sqlSel = CONCAT('SELECT COUNT(*) INTO ', rowSurnameName, ' FROM ', dbName, '.', tableName, ' WHERE COGNOME=', surnameNew, ' AND NOME=', nameNew);
PREPARE stmtSel FROM #sqlSel;
EXECUTE stmtSel;
DEALLOCATE PREPARE stmtSel;
IF (rowSurnameName=0) THEN
SET #sqlIns = CONCAT('INSERT INTO ', dbName, '.', tableName, ' (NOME, COGNOME) VALUES (', nameNew, ', ', surnameNew,')');
PREPARE stmtIns FROM #sqlIns;
EXECUTE stmtIns;
DEALLOCATE PREPARE stmtIns;
SELECT false INTO existsInDb;
ELSE SELECT true INTO existsInDb;
END IF;
END
The CALL Statement is:
SET #dbName = 'DBNAME';
SET #tableName = 'DBTABLE';
SET #surname = 'SURNAME';
SET #name = 'NAME';
PREPARE s FROM 'CALL InsertProc(?,?,?,?,?)';
EXECUTE s USING #existsInDB, #dbName, #tableName, #surname, #name;
SELECT #existsInDB;
And the ERROR Line is:
#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 'NULL' at line 1
A couple of notes:
You can't use a local variable in a prepared statement.
C.1 Restrictions on Stored Programs
...
SELECT ... INTO local_var cannot be used as a prepared statement.
...
The error shown in your question occurs because the local variable rowSurnameName has the value NULL, see:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> DECLARE `rowSurnameName` INT;
-> SELECT `rowSurnameName`;
-> SET #`sqlSel` := CONCAT('SELECT COUNT(*) INTO ', `rowSurnameName`);
-> SELECT #`sqlSel`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+------------------+
| `rowSurnameName` |
+------------------+
| NULL |
+------------------+
1 row in set (0.00 sec)
+-----------+
| #`sqlSel` |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
If you try to use the rowSurnameName local variable in the prepared statement, you will get the error:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> DECLARE `rowSurnameName` INT;
-> SET #`sqlSel` := CONCAT('SELECT 100 INTO `rowSurnameName`');
-> SELECT #`sqlSel`;
-> PREPARE `stmtSel` FROM #`sqlSel`;
-> EXECUTE `stmtSel`;
-> DEALLOCATE PREPARE `stmtSel`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+----------------------------------+
| #`sqlSel` |
+----------------------------------+
| SELECT 100 INTO `rowSurnameName` |
+----------------------------------+
1 row in set (0.00 sec)
ERROR 1327 (42000): Undeclared variable: rowSurnameName
You need to use 9.4 User-Defined Variables in your prepared statement:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> SET #`sqlSel` := CONCAT('SELECT 100 INTO #`rowSurnameName`');
-> SELECT #`sqlSel`;
-> PREPARE `stmtSel` FROM #`sqlSel`;
-> EXECUTE `stmtSel`;
-> DEALLOCATE PREPARE `stmtSel`;
-> IF (#`rowSurnameName` = 0) THEN
-> SELECT 'NotExistsInDbAndInsert';
-> ELSE
-> SELECT 'existsInDb';
-> END IF;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+-----------------------------------+
| #`sqlSel` |
+-----------------------------------+
| SELECT 100 INTO #`rowSurnameName` |
+-----------------------------------+
1 row in set (0.00 sec)
+------------+
| existsInDb |
+------------+
| existsInDb |
+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
(First post. Apologize if I breach any customs or protocol.)
I want to define an exit handler for any SQLWARNING thrown in my stored procedure. But the handler is being ignored, it never fires. And I am very confused.
I am using MariaDB v. 10.0.23. However I have tested this on MariaDB v. 10.1.14 and get the same result.
Below is the SQL to create a test database.
CREATE DATABASE testerrorhandling;
USE testerrorhandling;
CREATE TABLE test
(
int_notnull INT NOT NULL
);
DELIMITER //
CREATE DEFINER=CURRENT_USER PROCEDURE create_record
(
IN p INT
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'Handler for SQLEXCEPTION fired.';
DECLARE EXIT HANDLER FOR SQLWARNING
SELECT 'Handler for SQLWARNING fired.';
INSERT INTO test (int_notnull) VALUES (p);
END;
//
DELIMITER ;
If I attempt to add a null value, the procedure invokes the correct handler.
MariaDB [testerrorhandling]> call create_record(null);
+---------------------------------+
| Handler for SQLEXCEPTION fired. |
+---------------------------------+
| Handler for SQLEXCEPTION fired. |
+---------------------------------+
1 row in set (0.00 sec)
But now if I attempt to add a non-integer value, a warning is thrown but the SQLWARNING error handler never fires. A value of 0 is stored in the test table.
MariaDB [testerrorhandling]> call create_record('this is not an integer');
Query OK, 1 row affected, 1 warning (0.04 sec)
MariaDB [testerrorhandling]> show warnings;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
This is not what I expect or desire. I have to be doing something wrong. What am I missing here?
The warning occurs in the stored procedure call, not within the stored procedure, so the warning (within stored procedure) is not fired.
In the following example you can see that the parameter p has a value of zero (0) within the stored procedure, so there is no warning:
MariaDB [_]> DROP TABLE IF EXISTS test;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP PROCEDURE IF EXISTS create_record;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS test (
-> int_notnull INT NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE PROCEDURE create_record (
-> IN p INT
-> )
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLEXCEPTION
-> SELECT 'Handler for SQLEXCEPTION fired.';
->
-> DECLARE EXIT HANDLER FOR SQLWARNING
-> SELECT 'Handler for SQLWARNING fired.';
->
-> SELECT CONCAT('VALUE OF p: ', p);
-> INSERT INTO test (int_notnull) VALUES (p);
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> call create_record('this is not an integer');
+---------------------------+
| CONCAT('VALUE OF p: ', p) |
+---------------------------+
| VALUE OF p: 0 |
+---------------------------+
1 row in set (0.00 sec)
Query OK, 1 row affected, 1 warning (0.00 sec)
MariaDB [_]> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
In the following example, the warning is fired in the call to and within the stored procedure:
MariaDB [_]> DROP TABLE IF EXISTS test;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP PROCEDURE IF EXISTS create_record;
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS test (
-> int_notnull INT NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE PROCEDURE create_record (
-> IN p INT
-> )
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLEXCEPTION
-> SELECT 'Handler for SQLEXCEPTION fired.';
->
-> DECLARE EXIT HANDLER FOR SQLWARNING
-> SELECT 'Handler for SQLWARNING fired.';
->
-> SELECT CONCAT('VALUE OF p: ', p);
-> SET p := 'this is not an integer';
-> INSERT INTO test (int_notnull) VALUES (p);
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> call create_record('this is not an integer');
+---------------------------+
| CONCAT('VALUE OF p: ', p) |
+---------------------------+
| VALUE OF p: 0 |
+---------------------------+
1 row in set (0.00 sec)
+-------------------------------+
| Handler for SQLWARNING fired. |
+-------------------------------+
| Handler for SQLWARNING fired. |
+-------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [_]> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: 'this is not an integer' for column 'p' at row 1 |
+---------+------+---------------------------------------------------------------------------+
1 row in set (0.00 sec)
CREATE DEFINER=`root`#`localhost` PROCEDURE `Viewuser`(IN `userID` VARCHAR(50))
BEGIN
SELECT * FROM `tbl_userdetails` WHERE `UserID`=userID;
END
In the above code the user details of given user id should be return. But it returning the entire table while i execute this stored procedure.
try this method:
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)
mysql> delimiter ;
mysql> CALL simpleproc(#a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT #a;
+------+
| #a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
https://dev.mysql.com/doc/refman/5.5/en/create-procedure.html
Got the answer. The field name in table should not be same as variable name.. That is the problem
CREATE DEFINER=root#localhost PROCEDURE Viewuser(IN ID VARCHAR(50))
BEGIN
SELECT * FROM tbl_userdetails WHERE UserID=ID;
END
This is working fine
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 ;