参见写过的MySQL存储过程:MySQL存储过程删除历史数据_tabvla的博客-CSDN博客
在代码运行过程中,调用存储过程 call p_test("column name"); 反复遇到报错MySQL server version for the right syntax to use near 'NULL' at line 1,存储过程中相关代码部分如下:
DECLARE done INT DEFAULT FALSE;
DECLARE field_1 INT (11);
DECLARE p1 INT(11);
DECLARE cur_list CURSOR FOR (SELECT * FROM view_1);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP VIEW IF EXISTS view_1;
SET @strSql = CONCAT("CREATE VIEW view_1 as SELECT id from table1 WHERE column_1 = '", column1,"'");
PREPARE stmt FROM @strSql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
重点排查@strSql,当使用PREPARE时,如果sql语句中出现NULL,会导致报错,加入传参判断,完善CONCAT写法,顺利解决问题
SET @strSql = "CREATE VIEW view_1 as SELECT id FROM table1";
IF column1 IS NOT NULL THEN
SET @strSql = CONCAT(@strSql, " WHERE column_1 = '", column1,"'");
END IF;
PREPARE stmt FROM @strSql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
还挺意外,搜索了一下调用存储过程报null的错误非常常见,大概看了一下基本都是sql语句中NULL判断不够完善导致有可能出现空值,基本把空值可能性扼杀了问题就解决了,纠结怎么调用是没用的,问题出在存储过程里