Solved

How to extract table name from variable in PL/SQL

Posted on 2001-07-31
9
807 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 -- identify blocking session 24 52
Converting a row into a column 2 51
Query Records that don't match 8 42
having some issue on pl sql procedure 1 19
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

770 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