如果未在存储过程中创建,则创建数据库链接

时间:2013-04-26 20:22:38

标签: oracle stored-procedures database-link

我正在尝试在Oracle中编写存储过程。我没有很多经验,而且我遇到了一个问题。该过程的最终目标是从一个DB获取数据并以不同的形式将其放入另一个DB中。我有大部分程序似乎工作,但我遇到的问题似乎应该很简单。在代码的开头,我想检查以确保创建数据库链接。如果没有,那么我想创建数据库链接。

这就是我在程序中的内容:

IF (select count(1) from ALL_DB_LINKS where db_link = 'DB_LINK.NAME.COM') = 0 THEN
      CREATE DATABASE LINK LINK_NAME
      CONNECT TO username IDENTIFIED BY password
      USING 'SID';
END IF;

我知道该链接有效,因为我已在此之外完成并使用它进行查找。我尝试编译时得到的错误是:

  

在遇到以下任何一种情况时遇到符号“CREATE”:

我已经完成了所有谷歌搜索,我认为我可以做,我无法弄清楚我做错了什么。为了解决我遇到的另一个问题,我也尝试过这样做:

DECLARE test_count number;

select count(1) into test_count from ALL_DB_LINKS where db_link = 'DB_LINK.NAME.COM';
 BEGIN
  IF test_count = 0 THEN
      CREATE DATABASE LINK LINK_NAME
      CONNECT TO username IDENTIFIED BY password
      USING 'SID';
  END IF;
END;

但我得到同样的错误。我也不确定在开始内部开始是否有效。任何帮助都会很棒......好吧,帮助。

2 个答案:

答案 0 :(得分:2)

Oracle编译包,为了做到这一点,代码中引用的所有对象都必须已经存在。这包括通过数据库链接引用的对象。如果引用的对象不存在,事物将被标记为无效。通常,DBA将使用PL / SQL脚本而不是存储过程来创建和维护这样的环境。

如果你真的想要的话:

EXECUTE IMMEDIATE q'[ CREATE DATABASE LINK LINK_NAME
      CONNECT TO username IDENTIFIED BY password
      USING 'SID']';

答案 1 :(得分:0)

如果你真的需要这样做,你可以像Ed Gibbs建议的那样使用动态SQL创建链接,Brian表示,只要链接在创建过程时已经存在:

create database link test_link
connect to scott identified by oracle
using 'ORCL';

Database link created.

create or replace procedure p42 as
    l_count number;
    l_dummy dual.dummy%type;
begin
    select count(*) into l_count
    from all_db_links
    where db_link = 'TEST_LINK';

    if l_count = 0 then
        execute immediate q'[
            create database link test_link
            connect to scott identified by oracle
            using 'ORCL'
        ]';
    end if;
    select dummy into l_dummy from dual@test_link;
    dbms_output.put_line('Value from remote database: ' || l_dummy);
end;
/

Procedure created.

exec p42;

Value from remote database: X

PL/SQL procedure successfully completed.

但是如果该链接尚不存在,则该过程将无法编译:

drop database link test_link;

create or replace procedure p42 as
...
end;
/

Warning: Procedure created with compilation errors.

show errors

Errors for PROCEDURE P42:

LINE/COL ERROR
-------- -----------------------------------------------------------------
16/5     PL/SQL: SQL Statement ignored
16/5     PL/SQL: ORA-00942: table or view does not exist

奇怪的是,如果在链接存在时创建过程,则不反对,然后删除链接;该过程保持有效,动态SQL用于重新创建链接,并且过程成功运行:

drop database link test_link;

Database link dropped.

select object_type, object_name, status from all_objects
where object_type = 'PROCEDURE' and object_name = 'P42';

OBJECT_TYPE     OBJECT_NAME     STATUS
--------------- --------------- ---------------------
PROCEDURE       P42             VALID

exec p42;

Value from remote database: X

PL/SQL procedure successfully completed.

我原本希望删除链接以使程序无效,但显然不会,尽管它出现在all_dependencies中。您无法重新编译该过程:

drop database link test_link;

Database link dropped.

alter procedure p42 compile;

Warning: Procedure altered with compilation errors.

show errors

Errors for PROCEDURE P42:

LINE/COL ERROR
-------- -----------------------------------------------------------------
16/5     PL/SQL: SQL Statement ignored
16/5     PL/SQL: ORA-00942: table or view does not exist

为了让您在链接不存在时编译(或重新编译),您还需要使用动态SQL引用该链接:

drop database link test_link;

drop database link test_link
                   *
ERROR at line 1:
ORA-02024: database link not found

create or replace procedure p42 as
    ....
    execute immediate 'select dummy from dual@test_link' into l_dummy;
    dbms_output.put_line('Value from remote database: ' || l_dummy);
end;
/

Procedure created.

exec p42;

Value from remote database: X

PL/SQL procedure successfully completed.

drop database link test_link;

Database link dropped.

exec p42;

Value from remote database: X

PL/SQL procedure successfully completed.

drop database link test_link;

Database link dropped.

alter procedure p42 compile;

Procedure altered.

exec p42;

Value from remote database: X

PL/SQL procedure successfully completed.