if exist只能在存储过程里面用。SQL语句是不支持的。 要用也只能用case when 但是在SQL语句里也不能用drop,所以还是只能用存储过程来写。
begin if exists(select 1 from User_Tables t where t.table_name='test') then execute immediate 'drop table test'; end if; end;
哦,意思是说,如果想写个创建表的sql脚本的话,在脚本里先调用这个写好的这个存储过程,然后再写create table TEST 之语句是吗?
exists()这个函数只有sqlserver 里面有,oracle 里面没有exists() 这个函数 所以不能写 if exists(...)可以这么写: declare v_count number := 0 ; begin select 1 into v_count from User_Tables t where t.table_name=upper('test') ; execute immediate 'drop table test'; exception when no_data_found then dbms_output.put_line('There is no table before create;'); end ;
SP2-0734: 未知的命令开头 "if exists(..." - 忽略了剩余的行。
BEGIN immediate 'drop table TEST'; END; *
ERROR 位于第 1 行:
ORA-06550: 第 1 行, 第 17 列:
PLS-00103: 出现符号 "drop table TEST"在需要下列之一时:
:= . ( @ % ;
符号 ":=" 被替换为 "drop table TEST" 后继续。
SP2-0042: 未知命令"end if" -- 其余行忽略。
create table TEST (
*
ERROR 位于第 1 行:
ORA-00955: 名称已由现有对象使用
要用也只能用case when
但是在SQL语句里也不能用drop,所以还是只能用存储过程来写。
if exists(select 1 from User_Tables t where t.table_name='test') then
execute immediate 'drop table test';
end if;
end;
所以不能写 if exists(...)可以这么写:
declare v_count number := 0 ;
begin
select 1 into v_count from User_Tables t where t.table_name=upper('test') ;
execute immediate 'drop table test';
exception
when no_data_found then
dbms_output.put_line('There is no table before create;');
end ;