How to enter special characters like “\” in oracle database INSERT STATEMENT?
更新:
我刚刚发现问题不是来自反斜杠。问题是单引号问题。我正在尝试插入带有撇号的单词,只要列表中有一个,撇号就会被视为单引号。因此,即使我在原始插入值中没有反斜杠,Oracle 也会自动添加反斜杠。
示例:
INSERT INTO DICTIONNAIRE (Mot) 值 ('s\\\\'ensuivre');
原本是
INSERT INTO DICTIONNAIRE (Mot) 值 ('s'ensuivre');
有没有办法将撇号区别于值周围的单引号?
Original question:
I want to insert a backslash as a string:
1
2 INSERT INTO Dictionnaire (Mot,Definition) VALUES ('abasourdir','v. tr.\\\
Ahurir.');Is there a way to prevent the backslash from doing what it
normally does, that is:"escaping a single character or symbol", and
have the DBMS treat it like a simple string?set define off; doesn't work.
Thanks!
I want to insert a backslash as a string: INSERT INTO Dictionnaire (Mot,Definition) VALUES ('abasourdir','v. tr.\
Ahurir.');
仅当您将转义字符设置为反斜杠或在单个 SQL 语句中使用 ESCAPE 命令时,反斜杠才会用作转义字符。以免您应该能够像插入任何其他字符一样插入它。
使用两个反斜杠而不是一个反斜杠。
例如,我将转义字符设置为 set escape '\\\\':
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | SQL> CREATE TABLE t(a NUMBER, b VARCHAR2(10)); TABLE created. SQL> SQL> SET escape '\' SQL> SQL> INSERT INTO t VALUES(1, '\\\'); 1 row created. SQL> INSERT INTO t VALUES(2, '\\\ '); 1 row created. SQL> SQL> SELECT * FROM t; A B ---------- ---------- 1 \\ 2 \ SQL> |
Is there a way to prevent the backslash from doing what it normally does, that is:"escaping a single character or symbol", and have the DBMS treat it like a simple string?
那就不要用反斜杠转义了。将任何其他字符设置为转义字符。
例如,我将使用正斜杠作为转义字符而不是反斜杠:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | SQL> DROP TABLE t PURGE; TABLE dropped. SQL> SQL> CREATE TABLE t(a NUMBER, b VARCHAR2(10)); TABLE created. SQL> SQL> SET escape '/' SQL> SQL> INSERT INTO t VALUES(1, '\'); 1 row created. SQL> INSERT INTO t VALUES(2, '\ '); 1 row created. SQL> INSERT INTO t VALUES(2, '/n'); 1 row created. SQL> SQL> SELECT * FROM t; A B ---------- ---------- 1 \\ 2 \ 2 n SQL> |
所以,如您所见,只有正斜杠用于转义,而不是反斜杠。
就此而言,您也可以使用任何其他字符来转义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | SQL> SET escape '#' SQL> SQL> INSERT INTO t VALUES(1, '\'); 1 row created. SQL> INSERT INTO t VALUES(2, '\ '); 1 row created. SQL> INSERT INTO t VALUES(2, '#n'); 1 row created. SQL> SQL> SELECT * FROM t; A B ---------- ---------- 1 \\ 2 \ 2 n SQL> |
因此,您可以看到
Is there a way to treat the apostrophe differently than the single quotations surrounding the values?
1 | INSERT INTO DICTIONNAIRE (Mot) VALUES ('s'ensuivre'); |
是的,要在值中插入单引号,只需将其加倍即可。
1 | INSERT INTO DICTIONNAIRE (Mot) VALUES ('s''ensuivre'); |
1 2 3 4 5 | SQL> CREATE TABLE Dictionnaire(MOT VARCHAR2(20),Definition varchar2(40)); TABLE created. Elapsed: 00:00:00.71 |
这是如果你也想要引号,如果你只想要反斜杠然后看其他插入,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | SQL> INSERT INTO Dictionnaire (Mot,Definition) VALUES ('abasourdir','v. tr.''\''n Ahurir.'); 1 row created. Elapsed: 00:00:00.01 SQL> select * from Dictionnaire ; MOT DEFINITION -------------------- ---------------------------------------- abasourdir v. tr.'\'n Ahurir. Elapsed: 00:00:00.01 SQL> INSERT INTO Dictionnaire (Mot,Definition) VALUES ('abasourdir','v. tr.\ Ahurir.'); 1 row created. Elapsed: 00:00:00.01 SQL> select * from Dictionnaire ; MOT DEFINITION -------------------- ---------------------------------------- abasourdir v. tr.'\'n Ahurir. abasourdir v. tr.\ Ahurir. Elapsed: 00:00:00.01 |
我刚刚发现问题不是来自反斜杠。问题是单引号问题。我正在尝试插入带有撇号的单词,只要我的列表中有一个,撇号就会被视为单引号。因此,即使我在原始插入值中没有反斜杠,Oracle 也会自动添加反斜杠。
示例:
INSERT INTO DICTIONNAIRE (Mot) 值 ('s\\\\'ensuivre');
原本是
INSERT INTO DICTIONNAIRE (Mot) 值 ('s'ensuivre');
有没有办法将撇号区别于值周围的单引号?