列名以数字开头?

时间:2011-05-24 17:10:37

标签: sql oracle

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

的列名

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

  

ORA-00923:找不到FROM关键字。

如果每次带这个字段时,我怎么能把这个字段带进去而不会抛出错误?

3 个答案:

答案 0 :(得分:14)

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

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子句中遗漏了一个逗号。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

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: