在MySQL存储过程中使用条件WHERE子句设置自定义值以替代NULL值
若要在NULL值上设置自定义值,请使用UPDATE命令以及存储过程中的IS NULL属性。让我们首先创建一个表 –
mysql> create table DemoTable
(
Id int,
FirstName varchar(50)
);
Query OK, 0 rows affected (0.67 sec)
使用INSERT命令向表中插入一些记录 –
mysql> insert into DemoTable values(100,'Chris');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values(101,NULL);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values(102,'Mike');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable values(103,NULL);
Query OK, 1 row affected (0.74 sec)
使用SELECT语句从表中显示所有记录 –
mysql> select *from DemoTable;
这将产生以下输出 –
+------+-----------+
| Id | FirstName |
+------+-----------+
| 100 | Chris |
| 101 | NULL |
| 102 | Mike |
| 103 | NULL |
+------+-----------+
4 rows in set (0.00 sec)
以下是创建存储过程的查询语句 –
mysql> DELIMITER //
mysql> create procedure where_clauseProc()
BEGIN
update DemoTable set FirstName='Robert' where FirstName IS NULL;
END //
Query OK, 0 rows affected (4.11 sec)
mysql> DELIMITER ;
使用CALL命令调用存储过程 –
mysql> call where_clauseProc();
Query OK, 2 rows affected (0.10 sec)
使用SELECT语句从表中显示所有记录 –
mysql> select *from DemoTable;
这将产生以下输出 –
+------+-----------+
| Id | FirstName |
+------+-----------+
| 100 | Chris |
| 101 | Robert |
| 102 | Mike |
| 103 | Robert |
+------+-----------+
4 rows in set (0.00 sec)
阅读更多:MySQL 教程