Solved

how to deal with table variable in pl/sql

Posted on 2011-02-19
3
551 Views
Last Modified: 2012-08-14
Have a table T1.
I got the error:
PL/SQL: ORA-00942: table or view does not exist
when I executed the pl/sql statement:
declare
 str varchar2(10) := 'T1';
 cnt number(1);
begin
select count(1) into cnt from str;
  dbms_output.put_line(cnt);
end;
/

How to deal with it?  In real situation, the table name has to be a variable.
0
Comment
Question by:jl66
[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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
msd1305 earned 500 total points
ID: 34936016
declare
 str varchar2(10) := 'T1';
 cnt number;
begin

  execute immediate 'select count(1) from ' ||  str into cnt;
 
  dbms_output.put_line(cnt);
end;
0
 
LVL 4

Assisted Solution

by:msd1305
msd1305 earned 500 total points
ID: 34936032
Explanation: When you directly write a sql query, you can not use variable as a table name. Because database expects a table name STR .

Execute Immidiate expects sql query as a string and executes it. So not only table name but you can execute any kind of dynamic queries.

just google it to get more explanation :) .

Hope this helps.
0
 

Author Closing Comment

by:jl66
ID: 34936073
Excellent.
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

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.
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.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to recover a database from a user managed backup

751 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