-
Notifications
You must be signed in to change notification settings - Fork 111
list_add()
xiaoboluo768 edited this page Jun 9, 2020
·
2 revisions
-
将指定文本添加到一个指定名称的列表中,由两个传参定义,传入一个列表和一个字符串,返回值为在这个传入列表基础上添加了传入字符串的列表,如添加会话级别的sql_mode:set sql_mode=select sys.list_add(@@sql_mode,'ANSI_QUOTES');利用该函数来操作一些列表型系统变量可以减少人工赋值的失误
- 此函数和list_drop()函数一起可用于操作具有列表类型值的系统变量值的添加和删除,例如:sql_mode和optimizer_switch系统变量值具有一个逗号分隔的列表值
- 此函数在MySQL 5.7.9中新增
-
参数:
- in_list TEXT:要修改值的列表变量名称,注意:该参数只能传入变量类型值,不能是字符串值,详见函数定义语句注释部分示例
- in_add_value TEXT:要添加到列表变量中的具体字符串值
-
返回值:一个TEXT文本值
-
定义语句
DROP FUNCTION IF EXISTS list_add;
DELIMITER $$
CREATE DEFINER='root'@'localhost' FUNCTION list_add (
in_list TEXT,
in_add_value TEXT
)
RETURNS TEXT
COMMENT '
Description
-----------
Takes a list, and a value to add to the list, and returns the resulting list.
Useful for altering certain session variables, like sql_mode or optimizer_switch for instance.
Parameters
-----------
in_list (TEXT):
The comma separated list to add a value to
in_add_value (TEXT):
The value to add to the input list
Returns
-----------
TEXT
Example
--------
mysql> select @@sql_mode;
+-----------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> set sql_mode = sys.list_add(@@sql_mode, ''ANSI_QUOTES'');
Query OK, 0 rows affected (0.06 sec)
mysql> select @@sql_mode;
+-----------------------------------------------------------------------------------------------+
| @@sql_mode |
+-----------------------------------------------------------------------------------------------+
| ANSI_QUOTES,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
'
SQL SECURITY INVOKER
DETERMINISTIC
CONTAINS SQL
BEGIN
IF (in_add_value IS NULL) THEN
SIGNAL SQLSTATE '02200'
SET MESSAGE_TEXT = 'Function sys.list_add: in_add_value input variable should not be NULL',
MYSQL_ERRNO = 1138;
END IF;
IF (in_list IS NULL OR LENGTH(in_list) = 0) THEN
-- return the new value as a single value list
RETURN in_add_value;
END IF;
RETURN (SELECT CONCAT(TRIM(BOTH ',' FROM TRIM(in_list)), ',', in_add_value));
END$$
DELIMITER ;
上一篇: format_time()函数 | 下一篇: list_drop()函数
- 验证、测试、整理:罗小波
- QQ:309969177
- 提示:本系列文章的主体结构遵循Oracle MySQL 官方 5.7 手册中,关于information_schema、mysql schema、performance_schema、sys schema的章节结构体系,并额外添加了一些验证、测试数据。鉴于本人精力和能力有限,难免出现一些纰漏,欢迎大家踊跃指正!