Why am I getting this SQL/DB error?

﹥>﹥吖頭↗ 提交于 2019-12-08 03:06:22

问题


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

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:

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,


回答1:


May be this will be of help,
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

So, I would add semicolons and invoke with -t switch whatever nonsense it stands for.
I looked into samples, they use something like

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.




回答2:


You have a comma after the name line

Change:

NAME VARCHAR(20) NOT NULL,

to:

NAME VARCHAR(20) NOT NULL



回答3:


I would insert a space or a line separator between CREATE TABLE PERSONS and (
just to be safe.




回答4:


If I include semicolons and use the -t flag, I get:

SQL0104N  An unexpected token "/" was found following "BEGIN-OF-STATEMENT".  
Expected tokens may include:  "<values>".  SQLSTATE=42601



回答5:


The following syntax without spaces works right on the command line,

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



来源:https://stackoverflow.com/questions/568263/why-am-i-getting-this-sql-db-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!