Solved

How to extract table name from variable in PL/SQL

Posted on 2001-07-31
9
822 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

617 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