ORA-01756: 带引号的字符串未正确终止,我是否需要在 phone 数字上添加引号,为什么?

ORA-01756: quoted string not properly terminated, do i need to add quote on the phone number, why?

SQL> create table customer(
  2  customer_ID number(3) primary key,
  3  customer_Name varchar2(26),
  4  contact_Name  varchar2(26),
  5  phone number(10));

Table created.

SQL> desc customer
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 CUSTOMER_ID                               NOT NULL NUMBER(3)
 CUSTOMER_NAME                                      VARCHAR2(26)
 CONTACT_NAME                                       VARCHAR2(26)
 PHONE                                              NUMBER(10)
SQL> insert into customer 
        (customer_ID, customer_Name, contact_Name, phone) 
        values (23, 'Dave's Sub Shop', 'David Logan', 555-333-4545);
ERROR:
ORA-01756: quoted string not properly terminated

任何人都可以为我解释这个错误吗?

您需要在 Dave 的子商店中转义撇号。

这里有两个问题:

  • 您正在插入一个包含嵌入式单引号的字符串:'Dave's Sub Shop'。故障出现在任何体面的文本编辑器(包括 SO 的)中。您需要转义引号:'Dave''s Sub Shop'

  • phone_number 列声明为 number(10),但是您尝试插入的内容看起来不像:555-333-4545。您应该将列数据类型更改为 varchar(12)。然后您将需要引用您插入的值。

Demo on DB Fiddle :

create table customer(
    customer_ID number(3) primary key,
    customer_Name varchar2(26),
    contact_Name  varchar2(26),
    phone varchar(12));

insert into customer 
        (customer_ID, customer_Name, contact_Name, phone) 
        values (23, 'Dave''s Sub Shop', 'David Logan', '555-333-4545');

1 rows affected

select * from customer;

CUSTOMER_ID | CUSTOMER_NAME   | CONTACT_NAME | PHONE       
----------: | :-------------- | :----------- | :-----------
         23 | Dave's Sub Shop | David Logan  | 555-333-4545

NB :另一个解决方案是在插入之前将 phone 数字转换为数字数据类型(例如通过删除嵌入的 -),但我不推荐它,因为 phone 数字实际上是 而不是 数字:它们可能有有意义的前导零,并且可能包含非数字字符(()+、... )

你想要:

insert into customer (customer_ID, customer_Name, contact_Name, phone) 
    values (23, 'Dave''s Sub Shop', 'David Logan', '555-333-4545');

您的错误是由于 "Dave's Sub Shop"。第一个引号被当作字符串的结尾,所以错误发生在 s.

555-333-4545 不一定会产生错误。但它会产生数字 -4323,这不是您想要的。