0

I'm trying to code a stored procedure in Oracle. I don't have a ton of experience with it and I'm running to an issue. The end goal of the procedure is to take data from one DB and put it into another in a different form. I have most of the procedure working it seems but I'm having issues with something that seems like it should be simple. At the beginning of the code, I would like to check to make sure a DB link is created. If not, then I want to create the db link.

This is what I put inside my procedure:

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;

I know the link works because I've done it outside this and did a lookup with it. The error I get when I try to compile is this:

Encountered the symbol "CREATE" when expecting one of the following:

I've done all the googling I think I can do and I cannot figure out what I am doing wrong. To head off the other question I have, I've also tried to do it by putting in:

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;

But I get the same error. I'm also not sure if having a begin inside a begin will work. Any help would be a great... well, help.

2
  • 1
    You can't do DDL directly within a procedure in Oracle. You can get around this by using EXECUTE IMMEDIATE.
    – Ed Gibbs
    Apr 26, 2013 at 20:25
  • I think you'd also have to use dynamic SQL to query over the link; I can't test right now but I'm pretty sure the procedure won't compile if you're selecting from a link that doesn't exist in static SQL, and will be invalidated if it's subsequently dropped. Creating objects at runtime sounds like something you might want to rethink...
    – Alex Poole
    Apr 26, 2013 at 21:31

2 Answers 2

2

Oracle compiles packages and in order to do that all objects referenced in code need to already exist. This includes objects referenced over a database link. Things will get marked invalid if the referenced objects don't exist. Usually a DBA will create and maintain such an environment using PL/SQL scripts and not stored procedures.

If you really want to anyway:

EXECUTE IMMEDIATE q'[ CREATE DATABASE LINK LINK_NAME
      CONNECT TO username IDENTIFIED BY password
      USING 'SID']';
1
  • Yes, this is what I was looking for. I understand that this is not the best way to do it but right now this is the way I need to do it because of other circumstances. thanks for the help!
    – user940769
    Apr 29, 2013 at 12:41
0

If you really need to do this, you can create the link using dynamic SQL as Ed Gibbs suggested and Brian showed, as long as the link already exists at the point the procedure is created:

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.

But the procedure won't compile if the link doesn't already exist:

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

Curiously it doesn't object if you create the procedure while the link exists, and then drop the link; the procedure stays valid, the dynamic SQL works to recreate the link, and the procedure runs successfully:

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.

I would have expected dropping the link to invalidate the procedure, but apparently not, despite it appearing in all_dependencies. You can't recompile the procedure though:

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

In order to let you compile (or recompile) while the link does not exist, you'd need to reference the link using dynamic SQL as well:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.