可否在PL/SQL程序中动态建表?
我想在PL/SQL程序中动态判断是否存在TABLE_A,TABLE_B,TABLE_C,TABLE_D
四张表,如果不存在就CREATE,若已经存在就先DROP后CREATE
不知是否可行?
谢谢!
问题点数:50、回复次数:13Top
1 楼LGQDUCKY(飘)回复于 2004-12-03 13:56:34 得分 15
可以,
execute immediate 'create table TABLE_A (a varchar2(10))';Top
2 楼asia2158()回复于 2004-12-03 13:58:17 得分 0
可是如果有存在的,就要先DROP后建表,这个逻辑如何 用PL/SQL实现呢??Top
3 楼zhaokeke2004(男人·海洋)回复于 2004-12-03 13:58:50 得分 20
当然可行咯,给你个例子:
3. 本地动态SQL
a.
create or replace procedure dsql1
is
--v_cursor integer;
begin
execute immediate 'create table TABLE_A(name char(2))';
dbms_output.put_line('successful !');
exception
when others then
dbms_output.put_line('error !!!');
end;Top
4 楼zhaokeke2004(男人·海洋)回复于 2004-12-03 13:59:46 得分 0
就用drop table好了,不过你create table的时候一定要有create any table的权限,否则是不行的Top
5 楼asia2158()回复于 2004-12-03 14:07:10 得分 0
是这样的吗?
create or replace procedure dsql1
is
--v_cursor integer;
begin
execute immediate 'create table TABLE_A(name char(2))';
dbms_output.put_line('successful !');
exception
when others then
execute immediate 'drop table TABLE_A';
dbms_output.put_line('error !!!');
end;Top
6 楼asia2158()回复于 2004-12-03 14:08:05 得分 0
--v_cursor integer;
这句是什么意思呢?
还需要游标吗?Top
7 楼LGQDUCKY(飘)回复于 2004-12-03 14:10:13 得分 0
select count(*) into a from user_tables where table_name ='TABLE_A'
if a<1 then
execute immediate 'drop table TABLE_A';
else
execute immediate 'create table TABLE_A (a varchar2(10))';
end if;
Top
8 楼LGQDUCKY(飘)回复于 2004-12-03 14:11:03 得分 0
这个人家的注释,没有使用到Top
9 楼LGQDUCKY(飘)回复于 2004-12-03 14:12:11 得分 15
create or replace procedure dsql1
is
--v_cursor integer;
begin
execute immediate 'create table TABLE_A(name char(2))';
dbms_output.put_line('successful !');
exception
when others then
execute immediate 'drop table TABLE_A';
dbms_output.put_line('error !!!');
end;
不能这样,因为异常情况不一定是表存在
还是还是使用if 来判断Top
10 楼asia2158()回复于 2004-12-03 14:12:30 得分 0
好,我懂了,我去单位试试,
先谢过各位大侠!Top
11 楼sosolong(solong)回复于 2004-12-03 16:33:42 得分 0
注意: 别忘了在 drop table 前备份数据Top
12 楼asia2158()回复于 2004-12-04 15:40:29 得分 0
发生了很奇怪的事情:
create or replace procedure pro_yy
is
begin
execute immediate 'create table a007 as select b.sn from emp b';
end yy;
可是在PL/SQL中提示"编译成功",但在命令窗口EXEC PRO_YY;
后总提示:(1)ORA01031----"权限不足"
(2)ORA06512----在ZUO.PRO_YY, LINE 4
(3)ORA06512----在LINE 1
而后我检查了ZUO的权限,他已经有了"create any table"的权限!
why??
Top
13 楼asia2158()回复于 2004-12-04 16:09:38 得分 0
OH!
SORROY !
是我忘了授权!
现在成功了!
谢谢大家!Top




