0

I am trying to execute this procedure:

CREATE OR REPLACE PROCEDURE SP_DYNAMIC 
AS
  tbl_list VARCHAR2(2000);
  DBLINK   VARCHAR2(100);
  V_SQL    VARCHAR2(1000);
BEGIN
  DBLINK := 'SOME_LINK';
  V_SQL := 'SELECT table_name,table_owner FROM dba_tab_modifications@:DB_LINK';

  EXECUTE IMMEDIATE V_SQL USING DBLINK;

  COMMIT;

  Dbms_Output.PUT_LINE (TBL_LIST);
END;

But When I execute the stored procedure I get the error:

ORA-01729: database link name expected
ORA-06512: at "SYSTEM.SP_DYNAMIC"
ORA-06512: at line 2

Can somebody help me with what I am doing wrong here?

1 Answer 1

2

The reason it doesn't work is because you can't use a bind variable as the dblink. You get the same error when running the following:

select * from dual@:dblink;

If you absolutely must use dynamic sql, you'd have to concatenate the dblink name into the statement - but you'll have to be aware that you're now open to SQL Injection:

V_SQL := 'SELECT table_name,table_owner FROM dba_tab_modifications@'||DB_LINK;

Sign up to request clarification or add additional context in comments.

2 Comments

Fuether to this answer... you can use DMBS_ASSERT.QUALIFIED_SQL_NAME on the result of dba_tab_modifications@'||DB_LINK; " part of the string in order to minimise the sql injection vulnerability
Thanks, @James - that's something I was unaware of.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.