如何真正跳过某列的处理?

发布于 2024-07-23 05:53:30 字数 788 浏览 3 评论 0原文

为了将数据(从 CSV 文件)加载到 Oracle 数据库中,我使用 SQL*Loader。

在接收这些数据的表中,有一个名为 COMMENTSvarchar2(500) 列。 由于某些原因,我想忽略 CSV 文件中的此信息。 因此,我编写了这个控制文件:

Options (BindSize=10000000,Readsize=10000000,Rows=5000,Errors=100)
  Load Data
  Infile 'XXX.txt'
  Append into table T_XXX
  Fields Terminated By ';'
  TRAILING NULLCOLS
(
    ...
    COMMENTS FILLER,
    ...
)

这段代码似乎工作正常,因为数据库中的 COMMENTS 字段始终设置为 null

但是,如果在我的 CSV 文件中,有一条记录,其中相应的 COMMENTS 字段超过 500 个字符的限制,我会从 SQL*Loader 收到错误:

Record 2: Rejected - Error on table T_XXX, column COMMENTS.
Field in data file exceeds maximum length

有没有办法真的 排除对我的 COMMENTS 字段的处理?

In order to load data (from a CSV file) into an Oracle database, I use SQL*Loader.

In the table that receives these data, there is a varchar2(500) column, called COMMENTS.
For some reasons, I want to ignore this information from the CSV file.
Thus, I wrote this control file:

Options (BindSize=10000000,Readsize=10000000,Rows=5000,Errors=100)
  Load Data
  Infile 'XXX.txt'
  Append into table T_XXX
  Fields Terminated By ';'
  TRAILING NULLCOLS
(
    ...
    COMMENTS FILLER,
    ...
)

This code seems to work correctly, as the COMMENTS field in database is always set to null.

However, if in my CSV file I have a record where the corresponding COMMENTS field exceeds the 500 characters limit, I get an error from SQL*Loader:

Record 2: Rejected - Error on table T_XXX, column COMMENTS.
Field in data file exceeds maximum length

Is there a way to really exclude the processing of my COMMENTS fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2024-07-30 05:53:30

我无法重现你的问题。 我正在使用 Oracle 10.2.0.3.0 和 SQL*Loader 10.2.0.1。

这是我的测试用例:

SQL> CREATE TABLE test_sqlldr (
  2     ID NUMBER,
  3     comments VARCHAR2(20),
  4     id2 NUMBER
  5  );

Table created

控制文件:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments filler,
  id2
)

数据文件:

1;aaa;2
3;abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz;4
5;bbb;6

我正在使用命令 sqlldr userid=xxx/yyy@zzz control=test.ctl 并且我得到了所有没有错误的行:

SQL> select * from test_sqlldr;

        ID COMMENTS                    ID2
---------- -------------------- ----------
         1                               2
         3                               4
         5                               6

您可以尝试另一种方法,我使用以下控制文件得到了相同的期望结果:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments "substr(:comments,1,0)",
  id2
)

更新以下 Romaintaz 的评论:我再次研究它并设法得到与您相同的错误,当列超过 255 个字符。 这是因为 SQL*Loader 的默认数据类型是 char(255)。 如果您有一列包含更多数据,则必须指定长度。 以下控制文件解决了 300 个字符的列的问题:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments filler char(4000),
  id2
)

希望这有帮助,

--
文森特

I can't reproduce your problem. I'm using Oracle 10.2.0.3.0 with SQL*Loader 10.2.0.1.

Here is my test case:

SQL> CREATE TABLE test_sqlldr (
  2     ID NUMBER,
  3     comments VARCHAR2(20),
  4     id2 NUMBER
  5  );

Table created

Control file:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments filler,
  id2
)

data file:

1;aaa;2
3;abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz;4
5;bbb;6

I'm using the command sqlldr userid=xxx/yyy@zzz control=test.ctl and I'm getting all the rows without errors:

SQL> select * from test_sqlldr;

        ID COMMENTS                    ID2
---------- -------------------- ----------
         1                               2
         3                               4
         5                               6

You may try another approach, I'm getting the same desired result with the following control file:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments "substr(:comments,1,0)",
  id2
)

Update following Romaintaz's comment: I looked into it again and managed to get the same error as you when the size of the column exceeded 255 characters. This is because the default datatype of SQL*Loader is char(255). If you have a column with more data you will have to specify the length. The following control file solved the problem for a column with 300 characters:

LOAD DATA
INFILE test.data
INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'
TRAILING NULLCOLS
( id,
  comments filler char(4000),
  id2
)

Hope this Helps,

--
Vincent

明月松间行 2024-07-30 05:53:30

只是为了建议一个微小的改进,您可以尝试类似的操作:

LOAD DATA
IN FILE test.data INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'TRAILING NULLCOLS
(
 id,
 comments char(4000) "substr(:comments, 1, 200)", 
 id2)

现在您将获取所有评论的前 200 个字符(或您在其位置指定的任何数字) - 除非您的某些输入记录的评论字段值超过4000 个字符,其中它们将被加载程序拒绝,并出现前面提到的“超出最大长度”错误。 但假设这种情况很少见或并非如此,所有记录都将加载一些被截断为 200 个字符的评论。

如果您超过 char(4000),您将收到 SQL Loader 错误 - 您可以将这头野兽推到多远。

Just to suggest a tiny improvement, you might try something like:

LOAD DATA
IN FILE test.data INTO TABLE test_sqlldr
APPEND
FIELDS TERMINATED BY ';'TRAILING NULLCOLS
(
 id,
 comments char(4000) "substr(:comments, 1, 200)", 
 id2)

Now you'll grab the first 200 characters (or any number you specify in it's place) of all comments - unless some of your input records have values for the comments field that exceed 4000 characters, in which they'll be rejected by loader with the 'exceeds max length' error noted earlier. But assuming that's rare or not the case, all the records will load with some of the comments truncated to 200 chars.

If you go over char(4000) you'll get a SQL Loader error - there's a limit to how far you can push the beast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文