使用FIXED格式创建Db2外部表

fnatzsnv  于 2022-11-07  发布在  DB2
关注(0)|答案(1)|浏览(262)

我正在尝试创建/选择一个固定格式的Db2外部表,但没有成功。
以下是我的尝试:
来自IBM pdf文件(https://www.tridex.org/wp-content/uploads/Db2ExternalTables_Tridex.pdf第21页):

db2 "CREATE EXTERNAL TABLE TABLE1_FIXED (col1 int, col2 varchar(20)) USING (DATAOBJECT '/home/myuser/myfile.fixed' FORMAT FIXED LAYOUT(REF BYTES 1,col1 BYTES \@1, col2 varchar(20) BYTES 4) RECORDLENGTH \@1+6)"
DB21034E  The command was processed as an SQL statement because it was not a valid Command Line Processor command.  During SQL processing it returned:
SQL0007N  The statement was not processed because a character that is not supported in SQL statements was included in the SQL statement.  Invalid character: "\".  Text preceding the invalid character: "F BYTES 1,col1 BYTES". 
SQLSTATE=42601

我已经尝试了很多关于这个SQL查询的方法,但都没有成功。
我还尝试过从一个简单的表创建一个外部表:

[i1156@pc-l-0037(el040701_dev:) ~]$ db2 "create table TABLE (col1 char(1) not null)"
DB20000I  The SQL command completed successfully.

db2 "CREATE EXTERNAL TABLE TABLE_EXT (col1 char(1) not null) USING (DATAOBJECT '/home/myuser/TABLE.fixed' FORMAT FIXED LAYOUT(col1 char(1)) RECORDLENGTH 1)"
DB20000I  The SQL command completed successfully.

cat TABLE.fixed
abc

db2 "select * from TABLE_EXT"
SQL1476N  The current transaction was rolled back because of error "-5199".  
SQLSTATE=40506

外部表创建似乎没问题,但是当我在这个外部表上运行'select *'时,我得到了SQL 1476 N错误。
我希望有人能帮助我;谢谢你

pw136qt2

pw136qt21#

最后,我已经能够取得进展,解决原来的问题。
进入我的简单测试sql查询,我忘记指定'BYTES'选项后,列定义进入LAYOUT选项。
另外,我想我的输入文件“TABLE.fixed”需要有行结束字符,这是不是原来的情况。编辑它,并用LF分隔每行后,我可以运行“选择 *”对我的外部表。也许这是一个限制的外部表解析。
步骤如下:

cat TABLE.fixed
a
b
c

db2 "CREATE EXTERNAL TABLE TABLE_EXT (col1 char(1) not null) USING (DATAOBJECT '/home/myuser/TABLE.fixed' FORMAT FIXED LAYOUT(col1 char(1) BYTES 1))"

db2 "select * from TABLE_EXT"

COL1
----
a   
b   
c   

  3 record(s) selected.

关于另一个包含引用的SQL查询,我也已经能够解决这个问题,方法是用'&'替换'@'序列(下面的查询是根据我的需要修改的查询,具有空值):

cat table1_ASC.txt
N1          Nrow1                
N2          Nrow2                
N45         Y                    

db2 "CREATE EXTERNAL TABLE TABLE1_FIXED (col1 int, col2 varchar(20)) USING (DATAOBJECT '/home/myuser/table1_ASC.txt' FORMAT FIXED LAYOUT(ref BYTES 1, col1 int BYTES 11 nullif &1='Y', ref BYTES 1, col2 varchar(20) BYTES 20 nullif &3='Y'))"

db2 "select * from TABLE1_FIXED"

COL1        COL2                
----------- --------------------
          1 row1                
          2 row2                
          3 -                   

  3 record(s) selected.

相关问题