【问题标题】:how to passing variable from bash to sqlplus如何将变量从bash传递到sqlplus
【发布时间】:2018-03-21 09:39:38
【问题描述】:

我在 bash 中有以下代码:

#/!bin/bash


USERS=tom1,tom2,tom3

我想通过 sqlplus 从我的变量“USERS”中删除所有用户

#/!bin/bash

USERS=tom1,tom2,tom3

sqlplus -s system/*** <<EOF
*some code*
DROP USER tom1 CASCADE;
DROP USER tom2 CASCADE;
DROP USER tom2 CASCADE;
EOF

请帮忙

【问题讨论】:

    标签: linux oracle sqlplus


    【解决方案1】:

    使用 sql / plsql 使用 USERS 变量拆分数据库很复杂,需要了解 REGEXP 函数,我不建议您这样做。此外,它还需要动态 Sql 来执行drop 查询。

    我建议你在shell脚本中拆分它,创建一个脚本并在sqlplus中执行它

    USERS="tom1,tom2,tom3"
        echo ${USERS} | tr ',' '\n' | while read word; do
            echo "drop user $word;"
        done >drop_users.sql
    
    sqlplus -s system/pwd <<EOF
    #some code
    @drop_users.sql
    EOF
    

    【讨论】:

      【解决方案2】:

      除了从脚本文件的USERS 参数派生的 Oracle DB 的 DDL 命令外,您的代码是正常的,调用 userDrop.sh。为了与USERS 参数和DB 建立适当的关系,应生成一个额外的文件(drpUsr.sql),并在EOF 标签内调用,并带有@ 符号前缀。

      让我们通过vi 命令编辑:

      $ vi userDrop.sh
      
      #!/bin/bash
      USERS=tom1,tom2,tom3
      
      IFS=',' read -ra usr <<< "$USERS" #--here IFS(internal field separator) is ','
      for i in "${usr[@]}"; do
        echo "drop user "$i" cascade;"
      done > drpUsr.sql
      
      #--to drop a user a privileged user sys or system needed.    
      sqlplus / as sysdba <<EOF
      @drpUsr.sql;
      exit;
      EOF
      

      调用为

      $ . userDrop.sh
      

      不要忘记通过 DB 的 exit 命令返回命令提示符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        • 1970-01-01
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多