Oracle PL-SQL:将多个分隔文件导入表中

发布于 2025-01-01 11:20:02 字数 452 浏览 5 评论 0原文

我有多个文件(f1.log、f2.log、f3.log 等) 每个文件都有 ; & 中的数据。 = 分隔格式。 (新行由 ; 分隔,字段由 = 分隔)例如

f1 的数据:

1=a;2=b;3=c

f2 的数据:

1=p;2=q;3=r

我需要读取所有这些文件并将数据导入到表中格式:

filename  number  data

f1        1       a

f1        2       b

f1        3       c

f2        1       p
[...]

我是 SQL 新手。您能指导一下吗,该怎么办?

I have multiple files (f1.log, f2.log, f3.log etc)
Each file has the data in ; & = delimited format. (new lines are delimited by ; and fields are delimited by =) e.g.

data of f1:

1=a;2=b;3=c

data of f2:

1=p;2=q;3=r

I need to read all these files and import data into table in format:

filename  number  data

f1        1       a

f1        2       b

f1        3       c

f2        1       p
[...]

I am new to SQL. Can you please guide me, how can do it?

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

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

发布评论

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

评论(1

尴尬癌患者 2025-01-08 11:20:02

使用 SQL*Loader 将文件放入表中。假设您创建了一个类似于以下内容的表:

create table FLOG
(
  FILENAME   varchar2(1000)
 ,NUM        varchar2(1000)
 ,DATA       varchar2(1000)
);

那么您可以使用以下控制文件:

LOAD DATA
INFILE 'f1.log' "str ';'"
truncate INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
   filename constant 'f1'
   ,num  char 
   ,data char 
)

但是,每个文件都需要不同的控制文件。这可以通过使用 shell 脚本动态创建控制文件来完成。示例 shell 脚本可以是:

cat >flog.ctl <<_EOF
LOAD DATA
INFILE '$1.log' "str ';'"
APPEND INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
filename constant '$1'
,num  char
,data char
)
_EOF

sqlldr <username>/<password>@<instance> control=flog.ctl data=$1.log

保存为 flog.sh 然后可以像这样运行:

./flog.sh f1
./flog.sh f2

Use SQL*Loader to get the files into a table. Assuming you have a table created a bit like:

create table FLOG
(
  FILENAME   varchar2(1000)
 ,NUM        varchar2(1000)
 ,DATA       varchar2(1000)
);

Then you can use the following control file:

LOAD DATA
INFILE 'f1.log' "str ';'"
truncate INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
   filename constant 'f1'
   ,num  char 
   ,data char 
)

However, you will need a different control file for each file. This can be done by making the control file dynamically using a shell script. A sample shell script can be:

cat >flog.ctl <<_EOF
LOAD DATA
INFILE '$1.log' "str ';'"
APPEND INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
filename constant '$1'
,num  char
,data char
)
_EOF

sqlldr <username>/<password>@<instance> control=flog.ctl data=$1.log

Saved as flog.sh it can then be run like:

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