Solved

How to extract table name from variable in PL/SQL

Posted on 2001-07-31
9
801 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
 
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
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 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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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 syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now