存储过程问题,看看,有什么地方写的不对。谢谢各位。。。
第一次用oracle写存储过程。
表和存储过程如下:
-- 创建表A
CREATE TABLE testA (
f_id numeric NOT NULL,
col1 varchar2(20) NULL,
col2 varchar2(40) NULL
);
insert into testA(f_id, col1, col2) values(1, 'QQQQQ ', 'iw4424 ');
insert into testA(f_id, col1, col2) values(1, 'EEEEE ', 'iieirw ');
insert into testA(f_id, col1, col2) values(1, 'WWWWW ', 'kjsier ');
-- 创建表B
CREATE TABLE testB (
f_id numeric NOT NULL,
col1 varchar2(20) NULL,
col2 varchar2(40) NULL
);
insert into testB(f_id, col1, col2) values(1, 'BBBB1 ', 'CCCC1 ');
insert into testB(f_id, col1, col2) values(1, 'BBBB2 ', 'CCCC2 ');
-- 创建存储过程
create or replace procedure ora.up_getdetail(currentid in numeric) as
begin
-- 创建表testA的序列
create sequence seq_testA increment by 1 start with 1;
-- 创建表testB的序列
create sequence seq_testB increment by 1 start with 1;
-- 创建存放表testA的临时表tempA
CREATE TABLE tempA (
f_id numeric NOT NULL,
col1 varchar2(20) NULL,
col2 varchar2(40) NULL,
new_id numeric NOT NULL
);
-- 创建存放表testB的临时表tempB
CREATE TABLE tempB (
f_id numeric NOT NULL,
col1 varchar2(20) NULL,
col2 varchar2(40) NULL,
new_id numeric NOT NULL
);
insert into tempA select testA.f_id, testA.col1, testA.col2, seq_testA.nextval from testA where f_id := currentid;
insert into tempB select testB.f_id, testB.col1, testB.col2, seq_testB.nextval from testB where f_id := currentid;
select nvl(tempA.f_id, tempB.f_id) as f_id, tempA.col1 as COL1A, tempA.col2 as COL2A, tempB.col1 as COL1B, tempB.col2 as COL2B from tempA, tempB where tempA.new_id = tempB.new_id(+);
DROP sequence seq_testA;
DROP sequence seq_testB;
DROP table tempA;
DROP table tempB;
end;
我直接执行: exec up_getdetail(1);时,提示:
SQL> exec up_getdetail(1);
begin up_getdetail(1); end;
ORA-06550: 第 1 行, 第 7 列:
PLS-00905: 对象 ORA.UP_GETDETAIL 无效
ORA-06550: 第 1 行, 第 7 列:
PL/SQL: Statement ignored
请问是什么地方出问题呐???
[解决办法]
在过程里不可以直接创建序列和表
如果要创建需要这样
v_sql varchar2(100);
begin
v_sql := 'create table a (a varchar2(2),b number) ';
execute immediate v_sql;
[解决办法]
oracle中的临时表应该这样使用,你的用到的序列可以用rownum替代
execute immediate 'CREATE GLOBAL TEMPORARY TABLE T_TEMP (
COL1 VARCHAR2(10),
COL2 VARCHAR2(10)
) ON COMMIT delete ROWS ';
--
select rownum,t.* from t
[解决办法]
procedure中不能直接写dml或ddl语句
后面的select也要用execute immediate处理