shell脚本连接到DB并运行一些查询

时间:2013-10-16 23:01:45

标签: sql oracle shell

我正在尝试编写一个脚本,我可以将其作为dba用户连接到db并计算用户数。最终我想更新我的脚本以检查用户是否存在;杀了他们的会议;然后删除用户。但是目前我想从计算用户开始,只是为了确保我的脚本正在运行。

但看起来它没有连接到DB。

    echo
    echo "3> Resetting databases..."

    read -p "Enter Database Server Hostname: " db_host
    read -p "Enter Database SID: " db_sid
    read -p "Enter DBA User: " dba_usr
    read -p "Enter DBA password: " dba_pwd

    read -p "Enter Schema1 owner: " usr1
    read -p "Enter alternate user1 " usr2
    read -p "Enter alternate user2 " usr3

    sqlplus -s $dba_usr/$dba_pwd@$db_sid << EOF

            set timing off
            set feedback off
            set serveroutput off

            DECLARE
                    usr1 varchar2(10) := '$usr1';
                    usr2 varchar2(10) := '$usr2';
                    usr3 varchar2(10) := '$usr3';
                    total INTEGER;

                    TYPE user_arr IS VARRAY(3) OF VARCHAR2(20);
                    users user_arr;

            BEGIN
                    dbms_output.put_line('Entered inside this block...');

                    users := user_arr( upper(usr1),
                                       upper(usr2),
                                       upper(usr3) );

                    total := users.count;

                    FOR i in 1 .. total LOOP
                            dbms_output.put_line('Usernames are: '|| users(i));
                    END LOOP;

            END;
/
EOF

输出如下:

3> Resetting databases...
Enter Database Server Hostname: HOSTNAME
Enter Database SID: SID
Enter DBA User: DBA
Enter DBA password: pwd
Enter Schema1 owner: user1
Enter alternate user1 user2
Enter alternate user2 user3

获取输入值后,它不会执行任何操作。我不知道出了什么问题。试图解决它,但不能。任何人都可以找出问题吗?谢谢!

1 个答案:

答案 0 :(得分:2)

只需删除set serveroutput off

您正在指示SQL * Plus不输出任何内容。