Is it possible to use a database link when defining a cursor in a PL/SQL routine? We are looking to run a statement
against several databases from a central database and load the results into a table and thought that we could cursor through the names in link$ and somehow use them in the cursor definition to get the info that we need from the other databases. Any suggestions?
See if you can pull the link name you need from the dba_db_links view.
SQL> desc dba_db_links Name Null? Type -------------------------- -------- --------------- OWNER NOT NULL VARCHAR2(30) DB_LINK NOT NULL VARCHAR2(128) USERNAME VARCHAR2(30) HOST VARCHAR2(2000) CREATED NOT NULL DATECreate a cursor that reads all the links. Loop through the links cursor and dynamically create a select statement to pull the data you want back. If you're unfamiliar with dynamic SQL, see the Oracle docs and look at the chapter on Dynamic SQL (chapter 8).
An example of how you might build the dynamic SQL statment would be:
v_stmt := 'INSERT INTO central_db_table SELECT * FROM other_db_table@' || cursor_rec.db_link ; EXECUTE IMMEDIATE (v_stmt) ;
For More Information
- Dozens more answers to tough Oracle questions from Karen Morton are available.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig deeper on Using Oracle PL-SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.