Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Mike.

Asked: October 20, 2017 - 2:39 am UTC

Last updated: September 18, 2020 - 4:13 am UTC

Version: Oracle 12C

Viewed 1000+ times

You Asked

I have several scripts that are hard wired when creating a database link and all works well. The time has come though to take them to the next level and expand the user community. To do that the CREATE DATABASE LINK statement needs to be dynamically determine the host connect string at runtime. Reading the connect string is not the issue though.

Simply stated the 1st statement (hard coded) below works fine. The 2nd statement does not nor does the 3rd statement. Any ideas?

CREATE DATABASE LINK TempLink USING 'host_name.abc_xyz.com:1521/srvc_name.abc_xyx.com'; -- works fine

CREATE DATABASE LINK TempLink USING :Bind_Variable_1; -- fails, tried variable with and without quotes

EXECUTE IMMEDIATE :Bind_Variable_2; -- fails, variable = 1st statement

and Connor said...

This is not a database link issue, it is a DDL issue. You do not bind to DDL, eg

SQL> exec execute immediate 'create table :x (c int)' using 'MY_TABLE';
BEGIN execute immediate 'create table :x (c int)' using 'MY_TABLE'; END;

*
ERROR at line 1:
ORA-00903: invalid table name
ORA-06512: at line 1


So you can still use dynamic SQL...just without using binds.

SQL> variable x varchar2(100)
SQL> exec :x := 'MYTAB'

PL/SQL procedure successfully completed.

SQL> exec execute immediate 'create table '||:x||' (c int)';

PL/SQL procedure successfully completed.


Obviously as always, take extreme care with security when using dynamic SQL

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Solution Found - Dynamic Database Link Creation

Mike Blake, October 20, 2017 - 3:44 am UTC

Careful review of your response pointed me in the right direction. The issue was not the variables or all the needed quotes. It was in your use of EXEC EXECUTE IMMEDIATE.

For many many reasons I almost never user EXECUTE IMMEDIATE and have never used EXEC EXECUTE IMMEDIATE. Once I did that everything fell into place. Need to go back to the books.

Thanx again and a perfect solution.
Connor McDonald
October 21, 2017 - 1:10 am UTC

glad we could help

Follow Up Question

Gaurav Prasad, September 17, 2020 - 9:11 pm UTC

Would really appreciate your valuable guidance in this.

create or replace procedure "CREATE_DYMANIC_DB_LINK"
IS
BEGIN
EXECUTE IMMEDIATE 'CREATE DATABASE LINK "REPO_HOST_DB.WORLD" CONNECT TO "SCMOPS" IDENTIFIED BY "ScmOpsTools" USING ''(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=slc17yak.us.oracle.com)(Port=25784))(CONNECT_DATA=(SID=str25784)))''';
END;


On running this via PL/SQL Block:
begin
CREATE_DYMANIC_DB_LINK;
end;

I'm getting this Error :

ORA-01031: insufficient privileges
ORA-06512: at "SCMOPS.CREATE_DYMANIC_DB_LINK", line 8
ORA-06512: at line 2
ORA-06512: at "SYS.DBMS_SQL", line 1721

Can you please and suggest what could have gone wrond
Connor McDonald
September 18, 2020 - 4:13 am UTC

Go to our Resources section, and search for"Roles" in the Presentation section. You'll get a link about roles within a procedure.

This will explain the resource and describe the solution.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library