pgsql 存儲過程初識

表T1 {sql

id bigint primary key;fetch

t2_id bigint ;
io

}function

表T2 {test

id bigint primary key;select

path not null;
im

}存儲過程

其中T1的字段 t2_id 是T2 的外鍵 let

刪除T1 中某條記錄時,若T2中有相應記錄,則返回其path值 ; 若無相應 T2 記錄,則返回null;path

實現的存儲過程以下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;

相關文章
相關標籤/搜索