如何在DB2中创建存储过程

时间:2014-03-26 05:36:48

标签: sql oracle stored-procedures db2

你能帮我在DB2中创建一个Oracle下面的程序吗? DB2中也提供了具有列的相同表名,但是下面的脚本不起作用

CREATE OR REPLACE PROCEDURE sample_proc (ACCT_NO in CHAR,p_cursor out SYS_REFCURSOR)
is
BEGIN
 OPEN p_cursor FOR
select sampl1,sample2,sample3
from
table_test b
where
rec_id='A'
and sample3=ACCT_NO ;
END;

1 个答案:

答案 0 :(得分:0)

如果你想要一个返回更好的使用功能,这里有一些例子来获取集合

CREATE TABLE table_test
(
   sample1   VARCHAR2 (1000),
   sample2   VARCHAR2 (1000),
   sample3   VARCHAR2 (1000)
);

insert into table_test ( sample1,sample2 ,sample3)
values     ('daftest1','dsdtest1','sstsest3');  
insert into table_test ( sample1,sample2 ,sample3)
values     ('FAStest1','fstest1','sstsest3');   
insert into table_test ( sample1,sample2 ,sample3)
values     ('sdtest1','asdtest1','fstest3');   
insert into table_test ( sample1,sample2 ,sample3)
values     ('test2','test2','test123');

CREATE OR REPLACE TYPE TEST_REC                                                              
AS OBJECT (
   sample1 VARCHAR2(1000)
  ,sample2 VARCHAR2(1000)
  ,sample3 VARCHAR2(1000)
);

CREATE OR REPLACE TYPE TEST_REPORT_TABLE                                                              
AS TABLE OF TEST_REC;

CREATE OR REPLACE FUNCTION testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
 v_rec test_rec;
 v_test_report_table test_report_table := test_report_table();
BEGIN

FOR i IN (SELECT sample1,sample2,sample3
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no)
LOOP
      v_rec:=test_rec(NULL,NULL,NULL);
dbms_output.put_line(i.sample1);

      v_rec.sample1:=i.sample1;
      v_rec.sample2:=i.sample2;
      v_rec.sample3:=i.sample3;
      v_test_report_table.EXTEND;
      v_test_report_table(v_test_report_table.COUNT) :=v_rec;


END LOOP;


RETURN v_test_report_table;




END;

    select * from table(testing(p_acct_no=>'sstsest3'))

enter image description here

如果您不需要动态添加行,最好使用批量收集

CREATE OR REPLACE FUNCTION JSTRAUTI.testing (p_acct_no IN varchar2)
RETURN test_report_table
IS
 v_test_report_table test_report_table := test_report_table();


BEGIN

 SELECT test_rec(sample1,sample2,sample3)
 bulk collect into v_test_report_table
FROM table_test b
--where rec_id='A'
where sample3=p_acct_no;

RETURN v_test_report_table;

END;

结果相同。