Solved

How to extract table name from variable in PL/SQL

Posted on 2001-07-31
9
817 Views
Last Modified: 2008-03-10
I am attempting to declare a cursor which would get the table name for the FROM clause from a variable. How do I go about doing this? thanks.
0
Comment
Question by:kitsoon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 2

Expert Comment

by:vanmeerendonk
ID: 6337178
which db-version are you using?
0
 
LVL 4

Expert Comment

by:jtrifts
ID: 6337236
This is not possible using normal SQL in a PL7SQL block.

To do this sort of thing, you have to use dynamic/DBMS SQL.

You can use dbms sql from V7 onwards I believe...

For an example of this, you can refer to the section on DBMS sql in Feursteins PL/SQL book (the best Pl/SQL reference there is, in my opinion).

Alternatively search throush the PAQs for DBMS SQL (or just DBMS and you should get plenty of examples).

Regards,

JT
0
 
LVL 2

Expert Comment

by:RMZ
ID: 6337280
HI
can u give us example for what u need?
---rmz---
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Accepted Solution

by:
saschek earned 150 total points
ID: 6337450
If you've got 8.1.7, you could use native dynamic sql

e.g.:
create or replace package my_pack
as
TYPE     rc_type is REF CURSOR;
FUNCTION open_cursor (p_query VARCHAR2) return rc_type;
end;
/
create or replace package body my_pack
as
function open_cursor (
   p_query VARCHAR2)
   return rc_type
as
   cur my_pack.rc_type;
begin
   open cur for p_query;
   return cur;
end open_cursor;
end my_pack;
/
declare
   v_cur my_pack.rc_type;
   v_val NUMBER;
begin
   v_cur := my_pack.open_cursor('select 123 from dual');
   fetch v_cur into v_val;
   close v_cur;
   dbms_output.put_line(v_val);
end;
/
-- Sven
0
 
LVL 3

Expert Comment

by:myerpz
ID: 6337887
if you only have oracle 7, then something like this would work :-

-- start of script
create or replace procedure delete_from_table
      (in_table_name varchar2,
       in_pk         varchar2)
is
  c1 integer;
  c2 integer;
begin
   c1 := dbms_sql.open_cursor;
   dbms_sql.parse(c1, 'delete ' || in_table_name || ' where pk = ' || in_pk, dbms_sql.v7);
   c2 := dbms_sql.execute(c1);
   dbms_sql.close_cursor (c1);

exception
   when others then
        dbms_output.put_line(sqlerrm);
        dbms_sql.close_cursor (c1);
end delete_from_table;
/
-- end of script

and then in SQL*Plus :-

execute delete_from_table('table_name','primary key value');



hth

0
 
LVL 2

Expert Comment

by:RMZ
ID: 6340312
HI
U CAN DECLARE A CURSSOR TO
DECLARE
CURSOR CUR_TABLE AS
SELECT TNAME FROM TAB WHERE TABTYPE='TABLE';
---RMZ---
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7034697
Please update and finalize this old, open question.  Please:

1) Award points ... if you need Moderator assistance to split points, comment here with details please or advise us in Community Support with a zero point question and this question link.
2) Ask us to delete it if it has no value to you or others
3) Ask for a refund so that we can move it to our PAQ at zero points if it did not help you but may help others.

EXPERT INPUT WITH CLOSING RECOMMENDATIONS IS APPRECIATED IF ASKER DOES NOT RESPOND.

Thanks,
Moondancer - EE Moderator

P.S.  Please click your Member Profile, expand View Question History to navigate and update all your open and locked questions.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7055625
Recommended disposition:

    Split points between: saschek and myerpz

DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7057206
Thanks, Dan.

Points have been split, awaiting myerpz comments here:
Points for myerpz -> http://www.experts-exchange.com/jsp/qShow.jsp?qid=20308230

Moondancer - EE Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question