我写了个创建表和自动创建列的存储过程。。。表明和列的组合字符串外围传入。。如下
procedure pro_createtable
(
tname NVARCHAR2,
tCnames NVARCHAR2, --列拼的字符串 以 #;分号隔开 十以内的前加空格,在送参数时注意
r_result out integer,
r_message out varchar
)
as
v_tname nvarchar2(10);
v_sql varchar2(2000);
v_cnt int;
tnLen int;--列字符长度
tinterval int; --几列
v_i int;
temstr nvarchar2(10);
v_num int; -- 执行成功的行
v_seq nvarchar2(50);
begin
--根据tCnames拆分列
tnLen :=length(tCnames);
tinterval := trunc(tnlen/6);
v_i :=0;
SELECT COUNT(*) INTO v_cnt FROM User_Tables WHERE TABLE_NAME = UPPER(tname);
if v_cnt>0 then
--创建列
select count(*) into v_cnt from syslog;
else
--创建表(有id的)和列
v_tname:=tname;
v_sql:='create table '||v_tname||' ('||'id number not null) '||' tablespace ATOM';
begin
EXECUTE IMMEDIATE v_sql into v_num;
end;
if v_num >0 then
--创建序列
v_seq := 'seq_'||tname;
select count(*) into v_cnt from ALL_SEQUENCES WHERE SEQUENCE_NAME = UPPER(v_seq);
if v_cnt>0 then
begin
v_sql:='create sequence '||v_seq||' increment by 1 start with 1 nomaxvalue cache 20';
EXECUTE IMMEDIATE v_sql;
end;
end if;
--创建列
loop
if v_i < tinterval then
temstr:= substr(tCnames,v_i*tinterval+1,4);
end if;
v_i :=v_i+1;
end loop;
end if;
end if;
end;
procedure pro_createcolumn
(
tname NVARCHAR2,
tCname NVARCHAR2,
r_result out integer
)
as
v_sql varchar2(1000);
v_cnt int;
begin
SELECT COUNT(*) into v_cnt FROM USER_TAB_COLUMNS WHERE TABLE_NAME = UPPER(tname) AND COLUMN_NAME = UPPER(tCname);
if v_cnt = 0 then
v_sql:= 'ALTER TABLE '|| UPPER(tname)||' ADD '||UPPER(tCname)||' nvarchar2(50) ';
EXECUTE IMMEDIATE v_sql;
end if;
end;
但是在pl/sql中测试不通过。。。pl/sql中提示 ora 06546 DDL 语句在非法的上下文中执行