【问题标题】:How attach the SQL script by SQLPlus to the bash loop?如何通过 SQLPlus 将 SQL 脚本附加到 bash 循环?
【发布时间】:2020-02-18 09:26:54
【问题描述】:

我想通过 sqlplus 并基于路径中的文件名(path /to/files) 使用 SQLPlus 完成表 (Table_with_DB) 路径中的文件名。

我创建了 2 个单独的 SQL 文件 (clear.sql ; register.sql)

和 1 与 bash (bashloop.sh)

clear.sql

BEGIN 
 package.clear('Table_with_DB'); 
END;

register.sql

BEGIN 
 package.register('folderName' , '&1);
END;

bashloop.sh

for i in path/to/files;
 do
  sqlplus -s userid/password@ @clear.sql
  sqlplus -s userid/password@ @register.sql
 done

我希望查询清除 Table_with_DB 并使用 SQLPlus 将文件名从 path/to/files 传输到 Table_with_DB >

但实际上它不起作用:(

【问题讨论】:

    标签: bash oracle sqlplus


    【解决方案1】:

    循环中的示例 sqlplus。

    #!/bin/bash
    
    username=system
    password=passwordsystem
    tns_alias=esmd
    
    
    for i in /opt/oracle/example1/test*/file*.txt;
     do
    $ORACLE_HOME/bin/sqlplus   $username/$password@$tns_alias <<EOF
    SET SERVEROUTPUT ON SIZE 2000
        BEGIN
          dbms_output.put_line('$i');
        END;
    /
    EOF
    
    done;
    

    示例输出

    Connected to:
    Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    
    SQL> SQL>   2    3    4  /opt/oracle/example1/test2/file2.txt
    
    PL/SQL procedure successfully completed.
    
    SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    
    SQL*Plus: Release 11.2.0.3.0 Production on Tue Oct 22 13:59:24 2019
    
    Copyright (c) 1982, 2011, Oracle.  All rights reserved.
    
    
    Connected to:
    Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    
    SQL> SQL>   2    3    4  /opt/oracle/example1/test3/file3.txt
    
    PL/SQL procedure successfully completed.
    
    SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    
    SQL*Plus: Release 11.2.0.3.0 Production on Tue Oct 22 13:59:24 2019
    
    Copyright (c) 1982, 2011, Oracle.  All rights reserved.
    
    
    Connected to:
    Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    
    SQL> SQL>   2    3    4  /opt/oracle/example1/test4/file4.txt
    
    PL/SQL procedure successfully completed.
    
    SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
    oracle@esmd:~/example1>
    

    【讨论】:

      【解决方案2】:

      如果我明白你想要做什么,那么在你的 bashloop.sh 中你想用 $i 替换 db_name - 或者如果你的文件名是 ${i}空格。

      【讨论】:

      • 我更改了这个 db_name。我想使用 clear.sql 清除表 Table_with_DB 中的所有数据,并使用 register.sqlpath/to/files 发送新数据(文件名)
      猜你喜欢
      • 2022-01-20
      • 2011-08-20
      • 2018-12-31
      • 1970-01-01
      • 2020-02-27
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多