在PL / SQL中更改数据库

时间:2017-04-28 19:41:11

标签: database oracle plsql connection

以下PL / SQL代码尝试连接到Oracle 11.2.0.4.0数据库,执行命令并对同一Oracle版本的其他三个数据库重复连接和命令。代码是:

DECLARE
    PASSWORD VARCHAR2(20);
    PASS_WORD VARCHAR2(20);
    ENVIRONMENT CHAR(1) := 'T';
    USERID INTEGER(10);
    USER_ID_4_ACCTS_TO_BE_DELETED INTEGER(10);
    TYPE ARRAY_T IS VARRAY(4) OF VARCHAR2(4);
    ARRAY ARRAY_T := ARRAY_T('A','BB','CCC','DDDD'');
BEGIN
    USERID := &USER_ID; -- Get user's id from keyboard (assume no change)
    PASSWORD := &PASS_WORD; -- Get user's password from keyboard (assume no change)
    FOR i IN 1..ARRAY.COUNT LOOP
        CONNECT ARRAY(i) || '_UPDADM_' || USERID || '/' || PASSWORD || '.' || '@CM_CM' || ENVIRONMENT || ARRAY(i)
        CALL ADMIN_USERS.DROP_USER(USERID ,'UPDADM');
    END LOOP;
END;
/

第一个连接字符串如下所示:     CONNECT A_UPDADM_123456 /密码。@ CM_CMTA

抛出以下错误。

CONNECT ARRAY(i) || '_UPDADM_' || USERID || '/' || PASSWORD || '.' || '@CM_CM' || ENVIRONMENT || ARRAY(i)

*
ERROR at line 13:
ORA-06550: line 13, column 1:
PLS-00103: Encountered the symbol "CONNECT" when expecting one of the
following:
( begin case declare end exit for goto if loop mod null
pragma raise return select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
continue close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe purge

1 个答案:

答案 0 :(得分:0)

您无法重新连接到PL / SQL中的其他数据库。连接的数据库实例提供运行时环境,因此更改它就像一个shell脚本登录到不同的服务器中间脚本,或者一个正在运行的Java进程将其自身动态地交给另一个JVM。

Switch user in pl/sql code block in sqlplus sessions

AskTOM: How to change user dynamically

在12c中,Multitenant选项可能会为您提供更多选项,但这仍然类似于在同一数据库中作用于多个用户的DBA脚本,而不是在过程中创建全新的连接。