SQL 列名以数字开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6114193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Column Name beginning with a number?
提问by JK0124
I have a column name in one of my tables called: 3RD_DIAG_CODE - VARCHAR2 (10 Byte)
我的其中一个表中有一个列名,称为: 3RD_DIAG_CODE - VARCHAR2 (10 Byte)
When I try to run a query, it gives me the following error highlighting 3RD_DIAG_CODE
.
当我尝试运行查询时,它给了我以下错误突出显示3RD_DIAG_CODE
。
ORA-00923: FROM keyword not found where expected.
ORA-00923: FROM 关键字未在预期位置找到。
How can I bring this field in without it throwing an error every time I bring this field in?
如何在每次引入该字段时不抛出错误的情况下引入该字段?
回答by gsiems
If you are using column names that start with a number then you need to use double quotes. For example:
如果您使用以数字开头的列名,则需要使用双引号。例如:
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
Edit:As for the error message itself, you are probably (as BQ wrote) missing a comma from the select clause.
编辑:至于错误消息本身,您可能(如 BQ 所写)在 select 子句中遗漏了一个逗号。
回答by AllenG
Check your specification, but in SQL Server we would have to enclose that column name in square brackets: [3RD_DIAG_CODE]
检查您的规范,但在 SQL Server 中,我们必须将该列名括在方括号中: [3RD_DIAG_CODE]
回答by BQ.
You probably have two columns listed without a comma between them.
您可能列出了两列,但它们之间没有逗号。
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: