【问题标题】:Column Name beginning with a number?以数字开头的列名?
【发布时间】:2011-05-24 17:10:36
【问题描述】:

我的一个表中有一个列名:3RD_DIAG_CODE - VARCHAR2 (10 Byte)

当我尝试运行查询时,它给了我以下突出显示 3RD_DIAG_CODE 的错误。

ORA-00923:在预期的地方找不到 FROM 关键字。

我怎样才能在每次带入此字段时不引发错误的情况下引入此字段?

【问题讨论】:

标签: sql oracle


【解决方案1】:

如果您使用以数字开头的列名,则需要使用双引号。例如:

create table foo (
"3RD_DIAG_CODE" varchar2(10 byte) --make sure you use uppercase for variable name
);

insert into foo values ('abc');
insert into foo values ('def');
insert into foo values ('ghi');
insert into foo values ('jkl');
insert into foo values ('mno');
commit;

select * from foo;

3RD_DIAG_C
----------
abc
def
ghi
jkl
mno

select 3RD_DIAG_CODE from foo;

RD_DIAG_CODE
------------
       3
       3
       3
       3
       3

select "3RD_DIAG_CODE" from foo;

3RD_DIAG_C
----------
abc
def
ghi
jkl
mno

编辑:至于错误消息本身,您可能(如 BQ 所写)在 select 子句中缺少逗号。

【讨论】:

    【解决方案2】:

    检查您的规范,但在 SQL Server 中,我们必须将该列名括在方括号中:[3RD_DIAG_CODE]

    【讨论】:

    • 通过在我的字段名称周围加上双引号,我能够运行查询并为我提供所需的结果。例如:SELECT Fieldnames, "3RD_DIAG_CODE" as DiagCd3 From MyTable;
    • OP 已经表明他们正在使用 Oracle,因此提供 SQL Server 语法的解决方案没有帮助。在 StackOverflow 上,[sql] 标签仅表示查询语言,而不是 MS SQL Server。
    【解决方案3】:

    您可能列出了两列,它们之间没有逗号。

    create table t (id number primary key, 3d varchar2(30))
    Error at Command Line:1 Column:39
    Error report:
    SQL Error: ORA-00904: : invalid identifier
    00904. 00000 -  "%s: invalid identifier"
    
    
    create table t (id number primary key, "3d" varchar2(30));
    table T created.
    desc t
    Name Null     Type         
    ---- -------- ------------ 
    ID   NOT NULL NUMBER       
    3d            VARCHAR2(30) 
    
    
    > select id, 3d from t --[as @gsiem mentions: THIS IS BAD]
    ID                     3D       
    ---------------------- -------- 
    
    > select id, "3d" from t
    ID                     3d                             
    ---------------------- ------------------------------ 
    
    > select id, [3d] from t
    
    Error starting at line 7 in command:
    select id, [3d] from t
    Error at Command Line:7 Column:11
    Error report:
    SQL Error: ORA-00936: missing expression
    00936. 00000 -  "missing expression"
    *Cause:    
    *Action:
    > select id 3d from t
    
    Error starting at line 8 in command:
    select id 3d from t
    Error at Command Line:8 Column:10
    Error report:
    SQL Error: ORA-00923: FROM keyword not found where expected
    00923. 00000 -  "FROM keyword not found where expected"
    *Cause:    
    *Action:
    

    【讨论】:

    • 尝试在表格中添加一两行数据并重新运行您的选择——选择 3d 与选择“3d”的行为不同。
    • @gsiems,很有趣。完全没想到会有这种行为。 > 从双 XASDFASDF ADFFAFAFDASDF 中选择 3dxasdfasdf, 4adffafafdasdf --------- ------------- 3.0 4
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2014-11-02
    • 2020-07-07
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    相关资源
    最近更新 更多