Bug #10792 unexpected result of IF declaredVar IS NULL
Submitted: 22 May 2005 23:02 Modified: 2 Jun 2005 19:00
Reporter: Mark Johnson (Basic Quality Contributor) Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.0.4-beta-standard-log OS:Linux (linux)
Assigned to: Evgeny Potemkin CPU Architecture:Any

[22 May 2005 23:02] Mark Johnson
Description:
Declared variables test as NULL after a 'select ... into variable' statement.  However, if one does not test them, they work as though not NULLs.

How to repeat:
Pardon the ERRORS, I made a few typos.  I do not believe the errors are relevant to reproducing this bug, but I thought it best to be precise in what I did.

mysql> create database db1;
Query OK, 1 row affected (0.00 sec)

mysql> use db1;
Database changed
mysql> create table t1(pk INT PRIMARY_KEY AUTO_INCREMENT, n INT, s 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 'PRIMARY_KEY AUTO_INCREMENT, n INT, s VARCHAR(50))' at line 1
mysql> create table t1(pk INT PRIMARY KEY AUTO_INCREMENT, n INT, s VARCHAR(50));
Query OK, 0 rows affected (0.04 sec)

mysql> create table t2(pk INT PRIMARY KEY AUTO_INCREMENT, n INT, s VARCHAR(50));
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE p1(IN n INT)
    -> BEGIN
    -> DECLARE nVar INT;
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> DELIMITER $$
mysql> CREATE PROCEDURE p1(IN n INT)
    -> BEGIN
    -> DECLARE nVar INT;
    -> DECLARE sVar VARCHAR(50);
    -> SELECT n, s INTO nVar, sVar FROM t1 WHERE pk = n;
    -> IF nVar IS NULL THEN
    -> SELECT * FROM nVarIsNULL;
    -> END IF;
    -> INSERT t2 (n, s) VALUES (nVar, sVar);
    -> END;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE PROCEDURE p2(IN n INT)
    -> BEGIN
    -> DECLARE nVar INT;
    -> DECLARE sVar VARCHAR(50);
    -> SELECT n, s INTO nVar, sVar FROM t1 WHERE pk = n;
    -> INSERT t2 (n, s) VALUES (nVar, sVar);
    -> END$$
Query OK, 0 rows affected (0.01 sec)

mysql> DELIMITER ;
mysql> INSERT INTO t1 (n, s) VALUES (1, 'one'),(2, 'two'),(3, 'three');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t1;
+----+------+-------+
| pk | n    | s     |
+----+------+-------+
|  1 |    1 | one   |
|  2 |    2 | two   |
|  3 |    3 | three |
+----+------+-------+
3 rows in set (0.00 sec)

mysql> call p1(2);
ERROR 1146 (42S02): Table 'db1.nVarIsNULL' doesn't exist
mysql> SELECT * FROM t2;
Empty set (0.00 sec)

mysql> call p2(3);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM t2;
+----+------+-------+
| pk | n    | s     |
+----+------+-------+
|  1 |    3 | three |
+----+------+-------+
1 row in set (0.00 sec)

mysql> SELECT version;
ERROR 1054 (42S22): Unknown column 'version' in 'field list'
mysql> SELECT VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 5.0.4-beta-standard-log |
+-------------------------+
1 row in set (0.00 sec)

mysql>

Suggested fix:
I cannot suggest a fix.  However, the If xxx IS NOT NULL should produce an accurate result.
[24 May 2005 10:41] Vasily Kishkin
Tested on Win 2000 Sp4, MySQL server 5.0.6 beta
[2 Jun 2005 18:59] Evgeny Potemkin
In procedure p1() inside IF .. IS NULL THEN .. END IF select made from table nVarIsNULL.
And that table isn't provided. 
When server tries to execute this procedure, it tries to open all tables which will possible be used in it. Fails to open nVarIsNULL and reports that there is no such table. 
There is nothing wrong.

So, it's not a bug.