变量使用方法

最近更新时间: 2024-10-17 17:10:00

postgres=# CREATE OR REPLACE package b_4 IS    
    #定义变量
    v_1 int default 10;
    #定义结构体
    type v_struct is record(
        f1 int,
        f2 varchar
    );
    v_struct_1 v_struct;
    #定义游标,但不能定义游标变量
    type v_ref is ref cursor;

    #定义存储过程
    procedure setval(); 
    procedure echoval(); 
end;
/
CREATE OR REPLACE package body b_4 is
    #实现过程
    procedure setval() is       
    begin
        v_1:=random()*10;
        v_struct_1.f1:=random()*10;
        v_struct_1.f2:=md5(random()::text);
    end;    

    procedure echoval() is       
        v_rec record;
        v_ref_1 v_ref;
        type myvarchar is table of varchar;
        v_myvarchar_1 myvarchar:='{"tbase","pgxz"}';
    begin

        raise notice 'v_1 = %',v_1;
        raise notice 'v_struct_1 = %',v_struct_1;
        raise notice 'v_myvarchar = %',v_myvarchar_1;
        open v_ref_1 for select schemaname,tablename from pg_tables limit 2;
        loop
            fetch v_ref_1 into v_rec;
            exit when v_ref_1%notfound;
            raise notice 'v_rec = %',v_rec;
        end loop;        
    end;    
end;
/
#调用
postgres=# call b_4.setval();                   
CALL
postgres=# call b_4.echoval();                  
NOTICE:  v_1 = 8
NOTICE:  v_struct_1 = (9,618ad5bdc38b67af927e46d5ba2fca60)
NOTICE:  v_myvarchar = {tbase,pgxz}
NOTICE:  v_rec = (public,t1)
NOTICE:  v_rec = (squeeze,tables_internal)
CALL
postgres=#
postgres=# declare
    v_1 int:=b_4.v_1;   #定义包中变量    
begin  
    raise notice 'v_1 = %',v_1;
b_4.v_1:=11;
raise notice 'b_4.v_1 = %',b_4.v_1;
end;
/
NOTICE:  v_1 = 11
NOTICE:  b_4.v_1 = 11
DO