If you don't know the name of the database link at compile time, you'd need to use dynamic SQL. Regardless of whether you're using dynamic SQL or static SQL, though, a procedure can't just run a SQL statement. It would have to do something with the results. You could open a sys_refcursor
either as a local variable or as an OUT
parameter. You could iterate through the results, fetching the data and doing something with the results. The fact that you have declared a procedure rather than a function implies that you want to modify the database state somehow rather than simply returning a sys_refcursor
. But the name of the procedure implies that maybe you really want a function that returns a sys_refcursor
.
If you really want a function that returns a sys_refcursor
CREATE OR REPLACE FUNCTION getValues( p_dblink IN VARCHAR2 )
RETURN SYS_REFCURSOR
IS
l_rc SYS_REFCURSOR;
BEGIN
OPEN l_rc FOR 'SELECT col1 FROM table1@' || p_dblink;
RETURN l_rc;
END;