为什么我会收到此 SQL/DB 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/568263/
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
Why am I getting this SQL/DB error?
提问by barfoon
I am trying to run a simple SQL statement with DB2 and am having a few problems. I would like to have a single script in a txt/db2 file and have the engine process all of the commands
我正在尝试使用 DB2 运行一个简单的 SQL 语句,但遇到了一些问题。我想在 txt/db2 文件中有一个脚本,并让引擎处理所有命令
Here is the script:
这是脚本:
CONNECT TO MYDB
CREATE TABLE PERSONS(
PID SMALLINT NOT NULL,
NAME VARCHAR(20) NOT NULL
)
TERMINATE
When I run a db2 -f /pathtofile I get:
当我运行 db2 -f /pathtofile 时,我得到:
SQL0104N An unexpected token "(" was found following "CREATE TABLE PERSONS".
Expected tokens may include: "END-OF-STATEMENT". SQLSTATE=42601
What am I doing wrong? Is there something wrong with my script? Also, why is it working without ";" terminators at the end of my statements?
我究竟做错了什么?我的脚本有问题吗?另外,为什么它没有“;”就可以工作 在我的语句末尾有终止符吗?
Thank you,
谢谢,
回答by eugensk
May be this will be of help,
http://www.uc.edu/R/r25/documentation/Version3.2/install_instructions.pdf:
可能这会有所帮助,
http: //www.uc.edu/R/r25/documentation/Version3.2/install_instructions.pdf:
The scripts use a semi-colon (;) to terminate each SQL command. If you use the DB2 Command Line Processor, you should remember to use the “-t” flag.
...
If you do not use the -t flag, you will get errors such as the
following upon running the db2ct32.sql script:
create table “ACCOUNTS” (
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token “(“ was found following “ate table “ACCOUNTS””.
Expected tokens may include: “END-OF-STATEMENT”. SQLSTATE=42601
脚本使用分号 (;) 来终止每个 SQL 命令。如果您使用 DB2 命令行处理器,您应该记住使用“-t”标志。
...
如果您不使用 -t 标志,那么
在运行 db2ct32.sql 脚本时您将得到如下错误:
create table “ACCOUNTS” (
DB21034E 该命令被作为 SQL 语句处理,因为它不是
有效的命令行处理器命令。在 SQL 处理期间,它返回:
SQL0104N 意外标记“(”在“ate table”ACCOUNTS”之后被发现。
预期的标记可能包括:“END-OF-STATEMENT”。SQLSTATE=42601
So, I would add semicolons and invoke with -t switch whatever nonsense it stands for.
I looked into samples, they use something like
所以,我会添加分号并使用 -t switch 调用它代表的任何废话。
我查看了样本,他们使用了类似的东西
db2 -tf "pathtofile"
Also with
还有
db2 -tvf "pathtofile"
you might get more diagnostics.
Don't push proprietary soft to the limits, they are not that wide.
您可能会获得更多诊断信息。
不要将专有软件推到极限,它们没有那么宽。
回答by barfoon
If I include semicolons and use the -t flag, I get:
如果我包含分号并使用 -t 标志,我会得到:
SQL0104N An unexpected token "/" was found following "BEGIN-OF-STATEMENT".
Expected tokens may include: "<values>". SQLSTATE=42601
回答by asharma
The following syntax without spaces works right on the command line,
以下不带空格的语法在命令行上正常工作,
CREATE TABLE PERSONS (PID SMALLINT NOT NULL, NAME VARCHAR(20) NOT NULL)
CREATE TABLE PERSONS (PID SMALLINT NOT NULL, NAME VARCHAR(20) NOT NULL)
formatting a file with this and not the paragraph style worked for my assignment here - http://www.eecs.yorku.ca/course_archive/2016-17/F/3421/project/create/yrb-create.txt
用这个格式化文件而不是段落样式适用于我的作业 - http://www.eecs.yorku.ca/course_archive/2016-17/F/3421/project/create/yrb-create.txt
回答by Element
You have a comma after the name line
在名称行后面有一个逗号
Change:
改变:
NAME VARCHAR(20) NOT NULL,
to:
到:
NAME VARCHAR(20) NOT NULL
回答by eugensk
I would insert a space or a line separator between CREATE TABLE PERSONS and (
just to be safe.
为了安全起见,我会在 CREATE TABLE Persons 和 ( 之间插入一个空格或一个行分隔符。