Solved

how to deal with table variable in pl/sql

Posted on 2011-02-19
3
548 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
  • 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

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

830 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