-
Notifications
You must be signed in to change notification settings - Fork 109
quote_identifier()
xiaoboluo768 edited this page Jun 9, 2020
·
2 revisions
-
返回字符串作为引用的标识符,即给字符串前后加上反撇,对于已存在的反撇直接替换为两个反撇,当SQL语句文本中存在着保留字或者是反撇号(`)字符时,该函数可以快速帮助添加反撇
- 该函数在MySQL 5.7.14中新增
-
参数:
- in_identifier TEXT:要作为引用标识符的文本字符串
-
返回值:一个TEXT文本值
-
定义语句
DROP FUNCTION IF EXISTS quote_identifier;
DELIMITER $$
-- https://dev.mysql.com/doc/refman/5.7/en/identifiers.html
-- Maximum supported length for any of the current identifiers in 5.7.5+ is 256 characters.
-- Before that, user variables could have any length.
--
-- Based on Paul Dubois' suggestion in Bug #78823/Bug #22011361.
CREATE DEFINER='root'@'localhost' FUNCTION quote_identifier(in_identifier TEXT)
RETURNS TEXT CHARSET UTF8
COMMENT '
Description
-----------
Takes an unquoted identifier (schema name, table name, etc.) and
returns the identifier quoted with backticks.
Parameters
-----------
in_identifier (TEXT):
The identifier to quote.
Returns
-----------
TEXT
Example
-----------
mysql> SELECT sys.quote_identifier(''my_identifier'') AS Identifier;
+-----------------+
| Identifier |
+-----------------+
| `my_identifier` |
+-----------------+
1 row in set (0.00 sec)
mysql> SELECT sys.quote_identifier(''my`idenfier'') AS Identifier;
+----------------+
| Identifier |
+----------------+
| `my``idenfier` |
+----------------+
1 row in set (0.00 sec)
'
SQL SECURITY INVOKER
DETERMINISTIC
NO SQL
BEGIN
RETURN CONCAT('`', REPLACE(in_identifier, '`', '``'), '`');
END$$
DELIMITER ;
上一篇: ps_thread_trx_info()函数 | 下一篇: sys_get_config()函数
- 验证、测试、整理:罗小波
- QQ:309969177
- 提示:本系列文章的主体结构遵循Oracle MySQL 官方 5.7 手册中,关于information_schema、mysql schema、performance_schema、sys schema的章节结构体系,并额外添加了一些验证、测试数据。鉴于本人精力和能力有限,难免出现一些纰漏,欢迎大家踊跃指正!