如何在oracle中使用sqlldr有条件地加载数据

发布于 2024-12-06 02:16:36 字数 1380 浏览 2 评论 0原文

我有一个txt文件,如下所示。第一个 machine_no、第二个 emp_no、第三个 shift_type(1 表示进入,3 表示退出)、第四个 work_date、第四个是时间。

001,0000000021,01,2011/06/21,06:50,            
001,0000000026,01,2011/06/21,14:00,         
001,0000000018,01,2011/06/21,07:00,         
001,0000000021,03,2011/06/21,14:00,         
001,0000000018,03,2011/06/21,16:50,         
001,0000000026,03,2011/06/21,16:55,  

我想加载表中的数据。字段time1有时间如果 time_type 为 1,如果 time_type 为 则字段 time2 具有时间 3. 请告诉我如何将其包含在控制文件中。

预先感谢您的帮助..Macky。

下面是oracle中的txt文件和表。

表格如下:

desc data_trans;

 Name                            Null?    Type
 ------------------------------- -------- ----
 MACHIAN                                  VARCHAR2(4)
 YEAR                                     NUMBER(4)
 MONTH                                    VARCHAR2(2)
 WDAY                                     VARCHAR2(2)
 TIME1                                    VARCHAR2(5)
 TIME2                                    VARCHAR2(5)
 TIME3                                    VARCHAR2(2)
 SHIFT_NO                                 NUMBER(1)
 TIME_TYPE                                NUMBER(1)
 WORK_DATE                                DATE
 EMP_NO                                   VARCHAR2(10)

I have a txt file as follows. 1st machine_no,2nd emp_no, 3rd shift_type (1 for entry,3 for exit), 4th work_date, 4th is time.

001,0000000021,01,2011/06/21,06:50,            
001,0000000026,01,2011/06/21,14:00,         
001,0000000018,01,2011/06/21,07:00,         
001,0000000021,03,2011/06/21,14:00,         
001,0000000018,03,2011/06/21,16:50,         
001,0000000026,03,2011/06/21,16:55,  

I want to load data in the table. The field time1 to have time if
time_type is 1 and the field time2 to have time if time_type is
3. Please let me know how I can have this in the control file.

Thanks in advance for your help..Macky.

Below is the txt file and table in oracle.

The table as follows:

desc data_trans;

 Name                            Null?    Type
 ------------------------------- -------- ----
 MACHIAN                                  VARCHAR2(4)
 YEAR                                     NUMBER(4)
 MONTH                                    VARCHAR2(2)
 WDAY                                     VARCHAR2(2)
 TIME1                                    VARCHAR2(5)
 TIME2                                    VARCHAR2(5)
 TIME3                                    VARCHAR2(2)
 SHIFT_NO                                 NUMBER(1)
 TIME_TYPE                                NUMBER(1)
 WORK_DATE                                DATE
 EMP_NO                                   VARCHAR2(10)

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

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

发布评论

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

评论(2

霞映澄塘 2024-12-13 02:16:36

为什么不使用外部表来读取此文件,然后您可以简单地从中进行 SELECT 并在 SQL 中轻松执行您想要的任何条件转换。

注意:有一些假设,您似乎将“time_type”与“shift_type”互换,并且数据文件等中不存在表列。

-- Create the Oracle directory
CREATE OR REPLACE DIRECTORY file_dir AS '<physical-server-directory-path>';

-- Grant privs on the directory
GRANT READ, WRITE ON DIRECTORY file_dir TO <username>;

-- Create the external table
CREATE TABLE ext_data_table
(machine_no                VARCHAR2(4),
 emp                       VARCHAR2(10),
 shift_type                VARCHAR2(2),
 work_date                 VARCHAR2(10),
 time                      VARCHAR2(5)
)
  ORGANIZATION EXTERNAL (
   DEFAULT DIRECTORY file_dir 
    ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE
    BADFILE file_dir :'file.bad'
    LOGFILE file_dir :'file.log'
    DISCARDFILE file_dir :'file.dsc'
     FIELDS TERMINATED BY ','
     (
      machine_no                CHAR(4),
      emp                       CHAR(10),
      shift_type                CHAR(2),
      work_date                 CHAR(10),
      time                      CHAR(5)
     )
   LOCATION (
    FILE_DIR:'<filename>'
   )
  )
  NOPARALLEL;

-- Insert (and transform) your file data
INSERT INTO data_trans
(machian,
 year,
 month,
 wday,
 time1,
 time2,
 time3,
 shift_no,
 time_type,
 work_date,
 emp_no)
SELECT machine_no,              -- machian
       SUBSTR(work_date, 1, 4), -- year
       SUBSTR(work_date, 6, 2), -- month
       SUBSTR(work_date, 9, 2), -- wday
       (CASE TO_NUMBER(shift_type)
           WHEN 1
           THEN time
           ELSE NULL
         END),                  -- time1
       (CASE TO_NUMBER(shift_type)
           WHEN 3
           THEN time
           ELSE NULL
         END),                  -- time2
       NULL,                    -- time3    (You don't specify)
       NULL,                    -- shift_no (You don't specify)
       TO_NUMBER(shift_type),   -- time_type
       TO_DATE(work_date, 'YYYY/MM/DD'), -- work_date
       emp                      -- emp_no
  FROM ext_data_table;

从给出的信息来看,这是一个接近的近似值。

希望有帮助。

Why don't you use an external table to read this file and then you can simply SELECT from it and perform any conditional transformation you want to easily in SQL.

N.B. There are a few assumptions, you seem to interchange "time_type" with what seems to be "shift_type" and there are table columns that are not present in your data file etc.

-- Create the Oracle directory
CREATE OR REPLACE DIRECTORY file_dir AS '<physical-server-directory-path>';

-- Grant privs on the directory
GRANT READ, WRITE ON DIRECTORY file_dir TO <username>;

-- Create the external table
CREATE TABLE ext_data_table
(machine_no                VARCHAR2(4),
 emp                       VARCHAR2(10),
 shift_type                VARCHAR2(2),
 work_date                 VARCHAR2(10),
 time                      VARCHAR2(5)
)
  ORGANIZATION EXTERNAL (
   DEFAULT DIRECTORY file_dir 
    ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE
    BADFILE file_dir :'file.bad'
    LOGFILE file_dir :'file.log'
    DISCARDFILE file_dir :'file.dsc'
     FIELDS TERMINATED BY ','
     (
      machine_no                CHAR(4),
      emp                       CHAR(10),
      shift_type                CHAR(2),
      work_date                 CHAR(10),
      time                      CHAR(5)
     )
   LOCATION (
    FILE_DIR:'<filename>'
   )
  )
  NOPARALLEL;

-- Insert (and transform) your file data
INSERT INTO data_trans
(machian,
 year,
 month,
 wday,
 time1,
 time2,
 time3,
 shift_no,
 time_type,
 work_date,
 emp_no)
SELECT machine_no,              -- machian
       SUBSTR(work_date, 1, 4), -- year
       SUBSTR(work_date, 6, 2), -- month
       SUBSTR(work_date, 9, 2), -- wday
       (CASE TO_NUMBER(shift_type)
           WHEN 1
           THEN time
           ELSE NULL
         END),                  -- time1
       (CASE TO_NUMBER(shift_type)
           WHEN 3
           THEN time
           ELSE NULL
         END),                  -- time2
       NULL,                    -- time3    (You don't specify)
       NULL,                    -- shift_no (You don't specify)
       TO_NUMBER(shift_type),   -- time_type
       TO_DATE(work_date, 'YYYY/MM/DD'), -- work_date
       emp                      -- emp_no
  FROM ext_data_table;

From the info given this is a close approximation.

Hope it helps.

人生戏 2024-12-13 02:16:36

这在 SQL*Loader 中是不可能的。使用 外部表,然后使用 解码 函数到 旋转

This is not possible in SQL*Loader. Use an External Table, then copy it into your real table using the decode function to pivot.

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