我有写一个存储过程,要求传的参数是两张输入表的名字,一个输出表的名字,
两张输入表的字段,输出表的字段,根据输入表内的字段进行处理,将内容输出到输出表中,这样的需求,能存储过程写吗?
如
A表
a b
1 10
2 20
3 30
B表
a b
1 10
2 20
3 30
C表
a b
1 20
2 40
3 60这里A B 为输入表,C为输出表,这些都要做为参数传入,这个加法的动作,也要通过参数传入。请大家指点.
两张输入表的字段,输出表的字段,根据输入表内的字段进行处理,将内容输出到输出表中,这样的需求,能存储过程写吗?
如
A表
a b
1 10
2 20
3 30
B表
a b
1 10
2 20
3 30
C表
a b
1 20
2 40
3 60这里A B 为输入表,C为输出表,这些都要做为参数传入,这个加法的动作,也要通过参数传入。请大家指点.
as
begin
execute immediate 'insert into '||c||' select t1.a,t1.b+t2.b b from '||a||' t1,'||b||' t2 where t1.a=t2.a';
end;
已写入 file afiedt.buf 1 create or replace procedure insertPro(tab1 in varchar2,tab2 in varchar2,opera in varchar2)
2 as
3 strsql varchar2(100);
4 begin
5 execute immediate 'truncate table c';
6 strsql:='insert into c select t1.a,t1.b'||opera||'t2.b from ';
7 strsql:=strsql||tab1||' t1,'||tab2||' t2 where t1.a=t2.a';
8 execute immediate strsql;
9* end;
SQL> /过程已创建。SQL> exec insertPro('a','b','+');PL/SQL 过程已成功完成。SQL> select * from c; A B
---------- ----------
1 20
2 40
3 60SQL> exec insertPro('a','b','-');PL/SQL 过程已成功完成。SQL> select * from c; A B
---------- ----------
1 0
2 0
3 0SQL> exec insertPro('a','b','*');PL/SQL 过程已成功完成。SQL> select * from c; A B
---------- ----------
1 100
2 400
3 900
--insert into tb3 select t.a,t.b &oprea k.b from tb1 t,tb2 k where t.a=k.aSQL> create table tb1 as with tb as(select 1 a,10 b from dual union all
2 select 2,20 from dual union all
3 select 3,30 from dual) select * from tb
4 /表已创建。
SQL> create table tb2 as with tb as(select 1 a,10 b from dual union all
2 select 2,20 from dual union all
3 select 3,30 from dual) select * from tb
4 /表已创建。SQL> create table tb3 as select * from tb1 where 1<>1
2 /表已创建。SQL> create or replace procedure p_tb3_sum(v_tb1 varchar2,v_tb2 varchar2,v_tb3 varchar2,oprea varchar2)
2 as
3 begin
4 execute immediate 'insert into '||v_tb3||' select k.a,k.b'||oprea||'t.b from '||v_tb1||' k,'||v_tb2||' t '||' where k.a=t.a';
5 commit;
6 end;
7 /过程已创建。SQL> exec p_tb3_sum('tb1','tb2','tb3','+')PL/SQL 过程已成功完成。SQL> select * from tb3
2 / A B
---------- ----------
1 20
2 40
3 60
这时就有麻烦了,如果能将用户填写的字段集传入处理,如果有类似数组类型的
参数就行了。。