oracle删除一些表数据,然后释放表空间的存储过程

create or replace procedure ttt is
begin
delete from bbb where。。。。。。 ;
commit;
create table aaa as select * from bbb;
commit;
--删除表bbb所有数据
truncate table bbb;
commit;
--将临时表aaa的数据转移到bbb表中
insert into bbb select * from aaa;
commit;
--删除临时表aaa
drop table aaa;
commit;

但是如果这么做了,编译的时候会报错。
错误:PLS-00103: 出现符号 "CREATE"在需要下列之一时:
begin case declare end
exception exit for goto if loop mod null pragma raise return
select update while with <an identifier>
<a double-quoted delimited-identifier> <a bind variable> <<
close current delete fetch lock insert open rollback
savepoint set sql execute commit forall merge pipe
行:12
文本:create table temp as select * from EMR_COURSERECORD_LOGO;

commit;
end ttt;

如果把create那个注释掉,truncate也会报错。不知道是为什么。

我用的是oracle 10g。是不是不能在存储过程中加create和truncate呢?大家帮我看看什么问题啊?
我还用execute immediate试了一下,虽然编译通过,但是execute immediate里面的东西是不能运行的。
不需要大家粘贴复制一大堆东西,只需要大家复制到自己的PL/SQL看看,希望能得到完美答复,谢了大家。
谢谢ysyhyt和badkano,我觉得ysyhyt回答应该是对的,不能加动态SQL。
但是问题又来了,你能帮我修改一下我要写的存储过程吗?
因为str:= 'create table '||tname|| '('||c1||' char,'||c2||' char) ';
execute immediate str;
我有点看不懂啊,||是什么意思啊?

create or replace procedure ttt is
begin
delete from bbb where。。。。。。 ;
commit;
execute immediate 'create table aaa as select * from bbb';
commit;
--删除表bbb所有数据
execute immediate 'truncate table bbb';
commit;
--将临时表aaa的数据转移到bbb表中
insert into bbb select * from aaa;
commit;
--删除临时表aaa
execute immediate 'drop table aaa';
commit;
end;

顺便说一句,你之前为什么要delete表bbb里的数据呢?还有,你存储过程里没end
---------补充------
||是用来区分普通字段和变量字段的
他那个写法不和我这个一样吗?

他那个只不过把我单引号里的sql设置成了一个变量,叫str
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-07
将你要执行的语句拼好赋给一个varchar2变量sql_str,然后Execute Immediate sql_str;commit;
这样就好了,记住每一次execute后都commit一下,要不有的事务是没提交的。
第2个回答  2010-08-25
||是连接字符串的符号,如果你table name和字段名没有用变量就不用连接,直接写成:

str:='create table test(aa varchar2(10),bb number)';
execute immediate str;

其中test是你要建立的表名,aa和bb是字段名,这个请用你自己的表名和字段名替换.
第3个回答  2010-08-23
我以前也这样写过,告诉你,存储过程里面只能放动态的sql,不能直接放create 这种东西的。估计truncate也是不能放的。
不管你是10g还是9i,都是不能的。

看我以前写的,以及人家的回答
http://zhidao.baidu.com/question/175832970.html
第4个回答  2010-08-23
str:= 'create table '||tname|| '('||c1||' char,'||c2||' char) ';
execute immediate str;
等同于
execute immediate( 'create table t_name ' || '(c1 char,c2 char)');