Solved

How to extract table name from variable in PL/SQL

Posted on 2001-07-31
9
811 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Forms 10g: How-To Prevent a Commit of 1 Record in Master-Detail block. 3 56
Oracle Insert not working 10 33
oracle collections 2 22
UTL_FILE invalid file operation 5 30
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

821 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