简体   繁体   English

从 .txt 文件读取数据并将其插入到 Oracle SQL DB [PL/SQL, Unix 脚本]

[英]Read and insert data from .txt file to Oracle SQL DB [PL/SQL, Unix Scripting]

i have an input file: $home/dir/subdir/input.txt我有一个输入文件: $home/dir/subdir/input.txt

content of input.txt: input.txt 的内容:

123,0000,11111,3,1,X
124,0001,11112,3,1,Y
125,0002,,4,2,Y
129,0003,11114,4,2,X

I have a table where col1, col2, col3 cannot be null:我有一张表,其中 col1、col2、col3 不能是 null:

col1 col1 col2 col2 col3 col3 col4 col4 col5 col5 col6 col6 col7 col7 col8 col8 col9 col9
123 123 0000 0000 11111 11111 3 3 1 1 X X
124 124 0001 0001 11112 11112 3 3 1 1 Y
129 129 0003 0003 11114 11114 4 4 2 2 X X

How do I insert the contents of input.txt to the table accordingly?如何将input.txt的内容相应地插入到表中? using pl/sql and unix scripting使用 pl/sql 和 unix 脚本

You can use the regexp_substr function.您可以使用regexp_substr function。

In the following example, I used the regexp_like function to confirm it was in the given format.在下面的示例中,我使用了regexp_like function 来确认它是给定的格式。 Adjust it as you like.随心所欲地调整它。
Example:例子:

SELECT regexp_substr(string, '[^,]+', 1, 1) as col2,
       regexp_substr(string, '[^,]+', 1, 2) as col3, 
       regexp_substr(string, '[^,]+', 1, 3) as col4, 
       regexp_substr(string, '[^,]+', 1, 4) as col7,
       regexp_substr(string, '[^,]+', 1, 5) as col8,
       regexp_substr(string, '[^,]+', 1, 6) as col9,  
FROM   table
WHERE  REGEXP_LIKE(string, '[0-9]+,[0-9]+,[0-9]+,[0-9]+,[0-9]+,[A-Za-z]+');

Result:结果:

COL1 COL1 COL2 COL2 COL3 COL3 COL4 COL4 COL5 COL5 COL6 COL6 COL7 COL7 COL8 COL8 COL9 COL9
- - 123 123 0000 0000 11111 11111 - - - - 3 3 1 1 X X
- - 129 129 0003 0003 11114 11114 - - - - 4 4 2 2 X X
- - 124 124 0001 0001 11112 11112 - - - - 3 3 1 1 Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM