Unix Technical Forum

Using Variable in FROM portion in PL/SQL

This is a discussion on Using Variable in FROM portion in PL/SQL within the Oracle Miscellaneous forums, part of the Oracle Database category; --> I am trying to use a variable in the FROM portion of an SQL statement in order to process ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-07-2008, 09:48 AM
Jim C
 
Posts: n/a
Default Using Variable in FROM portion in PL/SQL

I am trying to use a variable in the FROM portion of an SQL statement
in order to process multiple tables through a cursor loop.

Everything works fine through the assignment of the v_office variable,
which has values like aub_poledata, bak_poledata, etc., and reflects
actual table names.

What am I doing incorrectly?

SET SERVEROUTPUT ON

DECLARE
CURSOR cOffice IS
SELECT code, name FROM pl_mapoffice ORDER BY 1,2;
v_rec cOffice%ROWTYPE;
v_office VARCHAR2(12);
v_type VARCHAR2(12);
v_count NUMBER(9);
BEGIN
OPEN cOffice;
FETCH cOffice INTO v_rec;
WHILE cOffice%FOUND
LOOP
FETCH cOffice INTO v_rec;

v_office := v_rec.code || '_poledata';

SELECT
ptype, COUNT(uniqueid) PoleCount
INTO
v_type, v_count
FROM
v_office
WHERE
NVL(dtnttmdt,0) <> 0
AND
NVL(ptype, 'NA') IN ('NA', 'WOOD');

DBMS_OUTPUT.PUT_LINE(v_rec.name || ', ' || v_type || ', ' ||
v_count);

END LOOP;
CLOSE cOffice;
END;
/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-07-2008, 09:49 AM
Rauf Sarwar
 
Posts: n/a
Default Re: Using Variable in FROM portion in PL/SQL


Originally posted by Jim C
> I am trying to use a variable in the FROM portion of an SQL statement
> in order to process multiple tables through a cursor loop.
>
> Everything works fine through the assignment of the v_office variable,
> which has values like aub_poledata, bak_poledata, etc., and reflects
> actual table names.
>
> What am I doing incorrectly?
>
> SET SERVEROUTPUT ON
>
> DECLARE
> CURSOR cOffice IS
> SELECT code, name FROM pl_mapoffice ORDER BY 1,2;
> v_rec cOffice%ROWTYPE;
> v_office VARCHAR2(12);
> v_type VARCHAR2(12);
> v_count NUMBER(9);
> BEGIN
> OPEN cOffice;
> FETCH cOffice INTO v_rec;
> WHILE cOffice%FOUND
> LOOP
> FETCH cOffice INTO v_rec;
>
> v_office := v_rec.code || '_poledata';
>
> SELECT
> ptype, COUNT(uniqueid) PoleCount
> INTO
> v_type, v_count
> FROM
> v_office
> WHERE
> NVL(dtnttmdt,0) 0
> AND
> NVL(ptype, 'NA') IN ('NA', 'WOOD');
>
> DBMS_OUTPUT.PUT_LINE(v_rec.name || ', ' || v_type || ',
> ' ||
> v_count);
>
> END LOOP;
> CLOSE cOffice;
> END;
> /



You cannot substitute table name with a variable (v_office) in the from
clause like that. you would have to use dynamic sql. Goto
http://tahiti.oracle.com and search for "EXECUTE IMMEDIATE" or
"DBMS_SQL" package.

Regards
/Rauf Sarwar

--
Posted via http://dbforums.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:58 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com