2017-02-02 11 views
0

このコードはエラーではありませんが、データの変更は何もしません。いくつかの株価テーブルから複数の更新数量を取得したいカーソルを使用した複数の更新pgsql

drop FUNCTION SP_PROSES_STOCK(noresep bigint, p_post_cd varchar); 
CREATE or replace FUNCTION SP_PROSES_STOCK(noresep bigint, p_post_cd varchar) 
RETURNS void 
LANGUAGE plpgsql 
as $function$ 
DECLARE cursorData refcursor; 
    v_item_cd varchar; 
    v_quantity numeric; 

begin 

open cursorData FOR 
select A.item_cd, A.quantity from trx_medical_resep B 
inner join trx_resep_data A on A.medical_resep_seqno = B.medical_resep_seqno 
where B.medical_resep_seqno = noresep; 
fetch next from cursorData into v_item_cd,v_quantity; 
while (found) 
loop 
    update inv_pos_item set quantity = quantity - v_quantity 
    where item_cd = v_item_cd and pos_cd = p_post_cd; 
end loop; 
close cursorData; 

END 
$function$ 

答えて

0

私はあなたの機能を短縮し、FORを使用するように機能を修正しました。また、私はデバッグのためのRAISE NOTICEを追加します。試してみることができます:

CREATE or replace FUNCTION SP_PROSES_STOCK(noresep bigint, p_post_cd varchar) 
RETURNS void 
LANGUAGE plpgsql 
as $function$ 
DECLARE v_cursor record; 

BEGIN 

    FOR v_cursor IN 
     SELECT A.item_cd, 
       A.quantity 
     FROM trx_medical_resep B 
     JOIN trx_resep_data A ON A.medical_resep_seqno = B.medical_resep_seqno 
     WHERE B.medical_resep_seqno = noresep 
    LOOP 
     RAISE NOTICE 'Changes for %', v_curosr; 
     UPDATE inv_pos_item 
      SET quantity = quantity - v_cursor.quantity 
     WHERE item_cd = v_cursor.item_cd 
      AND pos_cd = p_post_cd; 
    END LOOP; 
END 
$function$ 

デバッグ後、RAISE NOTICEを削除できます。

+0

OKおかげで、私はそれをしようとします – damard

0

このためにループは必要ありません。単一のUPDATE文ははるかに高速になります。

CREATE or replace FUNCTION SP_PROSES_STOCK(noresep bigint, p_post_cd varchar) 
    RETURNS void 
as 
$function$ 
begin 

    update inv_pos_item 
    set quantity = quantity - v.quantity 
    from (
    select A.item_cd, A.quantity 
    from trx_medical_resep B 
     join trx_resep_data A on A.medical_resep_seqno = B.medical_resep_seqno 
    where B.medical_resep_seqno = noresep 
) v 
    where item_cd = v.item_cd and pos_cd = p_post_cd; 

END; 
$function$ 
LANGUAGE plpgsql; 
+0

は、文字列\ rの追加があり、私のデータベース上のデータエラーがある、あなたのすべてに感謝し、私は方法がわかりませんそれは起こり得る、私はデータサンプルを入力するためにdbeaverを使用する – damard

関連する問題