pgsql 存储过程初识

表T1 {

id bigint primary key;

t2_id bigint ;

}

表T2 {

id bigint primary key;

path not null;

}

其中T1的字段 t2_id 是T2 的外键 

删除T1 中某条记录时,若T2中有相应记录,则返回其path值 ; 若无相应 T2 记录,则返回null;

实现的存储过程如下
create or replace function test( tid bigint) returns varchar as
$$         
declare
    cursor1 refcursor;
    result varchar;
begin
    select t1.t2_id from T t1 where t1.id = tid into result;
    if  result is null  then
        raise notice '11';
        open cursor1 for delete from T1 t1 where t1.id = tid returning null as path;
        fetch cursor1 into result;
        return result;
    else
        raise notice '22';
        open cursor1 for delete from T1 t1 using T2 t2 where t1.id = tid and t1.t2_id = t2.id returning t2.path as path;
        fetch cursor1 into result;
        return result;
    end if;
end
$$
language plpgsql;







你可能感兴趣的:(存储过程,pgsql)