日期:2014-05-17  浏览次数:20995 次

请教:oracle里面怎么判断表存在
我所了解的有两种方法
  if exists (select * from USER_TABLES where name = 'T2') then

  if (select count(*) from USER_TABLES where TABLE_NAME='T2')>0) then 
应该没错把?

但是我在PL/sql developer里面调试的时候

exists句提示:
Error: PLS-00204: function or pseudo-column 'EXISTS' may be used inside a SQL statement only
Line: 32
Text: if exists (select * from USER_TABLES where name = "T2") then

count句提示:
Error: PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:  
  ( - + case mod new not null others <an identifier>
  <a double-quoted delimited-identifier> <a bind variable> avg
  count current exists max min prior sql stddev sum variance
  execute forall merge time timestamp interval date
  <a string literal with character set specification>
  <a number> <a single-quoted SQL string> pipe
  <an alternatively-quoted string literal with character set specification>
  <an alternativ
Line: 30

请高手解答

------解决方案--------------------
exists 只能出现在sql语句中,可以这样写
declare
i number;
begin
select count(*) into i from USER_TABLES where name = 'T2';
if i>0 then
'要执行的语句'
end if;
end;



------解决方案--------------------
select count(*) into num from USER_TABLES where TABLE_NAME='T2'
if num>0 then
--存在
end if;