在Oracle DB中提取数据

时间:2013-09-11 06:49:51

标签: sql oracle stored-procedures extract

我在Oracle 11g中有一个名为Name:Customer

的表
ID|Name |Country
1 |Mark |USA
2 |Allan|UK
3 |James|USA
4 |Todd |UK
5 |Mike |UK

我有一个文本文件list.txt

ID
1
3
5

我需要在文本文件中导出结果

的Result.txt

1 |Mark |USA
3 |James|USA
5 |Mike |UK

基本上我需要在Customer表中提取记录ID 1,3,5并将其保存到文本文件(result.txt),这可以在存储过程或脚本中完成。

2 个答案:

答案 0 :(得分:0)

可以通过使用外部表从文件中获取数据并将其与表连接,并使用存储过程中的utl_file导出数据。

但是如果您使用shell或任何其他脚本语言,则直接从脚本调用查询并将数据推送到文件。这很简单。

答案 1 :(得分:0)

  1. 创建表格列表(ID NUMBER)以存储list.txt
  2. 的内容
  3. 使用命令sqlldr将list.txt导入表列表

    sqlldr db_user/db_password@DB_SERVICE control=D:\list.ctl

    list.ctl内容:

    load

    infile 'D://list.txt'

    truncate into table test

    fields terminated by '|'

    (id)

  4. 3.从oracle表中导出查询结果:    sqlplus -S db_user/db_password@DB_SERVICE @D:\run.sql

    run.sql内容:

    `SET echo OFF`
    
    `SET feedback OFF`
    
    `SET pagesize 0`
    
    `SET term OFF`
    
    `SET trims ON`
    
    `SET timing OFF`
    
    `SET verify OFF`
    
    `SPOOL D:\result.txt`
    
    `select a.id || '|' || a.name || '|' || a.Country From Customer a, list b`
    
    `where a.id = b.id;`
    
    `SPOOL OFF`
    
    `quit`