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;

相关文章
相关标签/搜索