VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

转义符

功能描述

Vastbase在MySQL兼容模式下支持以下转义字符标志:

转义符 转义之后的字符
\" "
\n 换行符
\r 回车符
\t 制表符
\b 退格符
\' '
\\ \

注意事项

  • 该功能仅在数据库兼容模式为MySQL时支持(即数据库实例初始化时指定DBCOMPATIBILITY='B')。

  • 此特性支持的Vastbase版本:

    • V2.2 Build 10 (Patch No.15)及以后补丁版本。
    • V2.2 Build 15及以后版本。

语法格式

示例

示例1: 在insert中使用转义符。

1、创建测试表。

create table table_1173698(id int, c1 text, c2 text, c3 int );

2、向测试表插入数据。

insert into table_1173698 values (1,'测试abc123','噼里\t啪啦aaa',8388607);
insert into table_1173698 values (2,'测试abc123','噼里\n啪啦aaa',8388607);
insert into table_1173698 values (3,'测试abc123','噼里\r啪啦aaa',8388607);
insert into table_1173698 values (4,'测试abc123','噼里\b啪啦aaa',8388607);
insert into table_1173698 values (5,'测试abc123','噼里\0啪啦aaa',8388607);
insert into table_1173698 values (6,'测试abc123','噼里\\啪啦aaa',8388607);
insert into table_1173698 values (7,'测试abc123','噼里\"啪啦aaa',8388607);
insert into table_1173698 values (8,'测试abc123','噼里\'啪啦aaa',8388607);
insert into table_1173698 values (9,'测试abc123','\b',8388607);
insert into table_1173698 values (10,'测试abc123','\r',8388607);
insert into table_1173698 values (11,'测试abc123','\t',8388607);
insert into table_1173698 values (12,'测试abc123','\n',8388607);
insert into table_1173698 values (13,'测试abc123','\0',8388607);
insert into table_1173698 values (14,'测试abc123','\\',8388607);
insert into table_1173698 values (15,'测试abc123','\"',8388607);
insert into table_1173698 values (16,'测试abc123','\'',8388607);

3、查询结果。

select * from table_1173698;

返回结果为:

 id |     c1     |       c2        |   c3
----+------------+-----------------+---------
  1 | 测试abc123 | 噼里    啪啦aaa | 8388607
  2 | 测试abc123 | 噼里           +| 8388607
    |            | 啪啦aaa         |
  3 | 测试abc123 | 噼里\r啪啦aaa   | 8388607
  4 | 测试abc123 | 噼里\x08啪啦aaa | 8388607
  5 | 测试abc123 | 噼里            | 8388607
  6 | 测试abc123 | 噼里\啪啦aaa    | 8388607
  7 | 测试abc123 | 噼里"啪啦aaa    | 8388607
  8 | 测试abc123 | 噼里'啪啦aaa    | 8388607
  9 | 测试abc123 | \x08            | 8388607
 10 | 测试abc123 | \r              | 8388607
 11 | 测试abc123 |                 | 8388607
 12 | 测试abc123 |                +| 8388607
    |            |                 |
 13 | 测试abc123 |                 | 8388607
 14 | 测试abc123 | \               | 8388607
 15 | 测试abc123 | "               | 8388607
 16 | 测试abc123 | '               | 8388607
(16 rows)

示例2: 在存储过程中使用转义符。

1、创建测试表并插入数据。

create table table_1174629(id int, c1 text, c2 text, c3 int );
insert into table_1174629 values (1,'测试abc123','1噼里\t啪啦aaa',8388607);
insert into table_1174629 values (2,'测试abc123','2噼里\n啪啦aaa',8388607);
insert into table_1174629 values (3,'测试abc123','3噼里\r啪啦aaa',8388607);
insert into table_1174629 values (4,'测试abc123','4噼里\b啪啦aaa',8388607);
insert into table_1174629 values (5,'测试abc123','5噼里\0啪啦aaa',8388607);
insert into table_1174629 values (6,'测试abc123','6噼里\\啪啦aaa',8388607);
insert into table_1174629 values (7,'测试abc123','7噼里\"啪啦aaa',8388607);
insert into table_1174629 values (8,'测试abc123','8噼里\'啪啦aaa',8388607);

2、创建存储过程。

create or replace procedure proc_insert_1174629 (id int, c1 text, c2 text, c3 int)
as
begin
insert into table_1174629 value(id, c1, c2, c3);
end ;
/

create or replace procedure proc_update_1174629 ()
as
begin
update table_1174629 set c1='修改后\t abc123' where c2 = '1噼里\t啪啦aaa';
update table_1174629 set c1='修改后\r abc123' where c2 ='2噼里\n啪啦aaa';
update table_1174629 set c1='修改后\n abc123' where c2 ='3噼里\r啪啦aaa';
update table_1174629 set c1='修改后\b abc123' where c2 ='4噼里\b啪啦aaa';
update table_1174629 set c1='修改后\0 abc123' where c2 ='5噼里\0啪啦aaa';
update table_1174629 set c1='修改后\\ abc123' where c2 ='6噼里\\啪啦aaa';
update table_1174629 set c1='修改后\"abc123' where c2 ='7噼里\"啪啦aaa';
update table_1174629 set c1='修改后\' abc123' where c2 ='8噼里\'啪啦aaa';
end ;
/

3、打开serveroutput参数(使信息从存储过程传输回应用程序,输出在屏幕上)。

SET SERVEROUTPUT ON;

4、调用proc_insert_1174629存储过程并查询结果。

call proc_insert_1174629(9,'新\b插入\t测试\0abc123','8噼里\'啪啦aaa',8388607);
call proc_insert_1174629(10,'新\r插入\n测试\"abc123','8噼里\'啪啦aaa',8388607);
call proc_insert_1174629(11,'新\\插入\n测试\'abc123','8噼里\'啪啦aaa',8388607);
select * from table_1174629 order by id;

返回结果为:

 id |          c1          |        c2        |   c3
----+----------------------+------------------+---------
  1 | 测试abc123           | 1噼里   啪啦aaa  | 8388607
  2 | 测试abc123           | 2噼里           +| 8388607
    |                      | 啪啦aaa          |
  3 | 测试abc123           | 3噼里\r啪啦aaa   | 8388607
  4 | 测试abc123           | 4噼里\x08啪啦aaa | 8388607
  5 | 测试abc123           | 5噼里            | 8388607
  6 | 测试abc123           | 6噼里\啪啦aaa    | 8388607
  7 | 测试abc123           | 7噼里"啪啦aaa    | 8388607
  8 | 测试abc123           | 8噼里'啪啦aaa    | 8388607
  9 | 新\x08插入      测试 | 8噼里'啪啦aaa    | 8388607
 10 | 新\r插入            +| 8噼里'啪啦aaa    | 8388607
    | 测试"abc123          |                  |
 11 | 新\插入             +| 8噼里'啪啦aaa    | 8388607
    | 测试'abc123          |                  |
(11 rows)

5、调用proc_update_1174629存储过程并查询结果。

call proc_update_1174629();
select * from table_1174629 order by id;

返回结果为:

 id |        c1         |        c2        |   c3
----+-------------------+------------------+---------
  1 | 修改后   abc123   | 1噼里   啪啦aaa  | 8388607
  2 | 修改后\r abc123   | 2噼里           +| 8388607
    |                   | 啪啦aaa          |
  3 | 修改后           +| 3噼里\r啪啦aaa   | 8388607
    |  abc123           |                  |
  4 | 修改后\x08 abc123 | 4噼里\x08啪啦aaa | 8388607
  5 | 修改后            | 5噼里            | 8388607
  6 | 修改后\ abc123    | 6噼里\啪啦aaa    | 8388607
  7 | 修改后"abc123     | 7噼里"啪啦aaa    | 8388607
  8 | 修改后' abc123    | 8噼里'啪啦aaa    | 8388607
  9 | 修改后' abc123    | 8噼里'啪啦aaa    | 8388607
 10 | 修改后' abc123    | 8噼里'啪啦aaa    | 8388607
 11 | 修改后' abc123    | 8噼里'啪啦aaa    | 8388607
(11 rows)