Oracle将一个表的值更新为另一个表字段的过程

时间:2014-06-04 17:54:34

标签: sql oracle stored-procedures oracle11g

我有两个带有两个属性的表。我编写了一个过程,它应该从table1获取值并在表2中更新它。

MEDICINE 

BARCODE varchar2 13 byte
PDF_KUB_PATH  varchar2 1000 byte

PARSED_ILAC_REHBERI

BARCODE varchar2 13 byte
PDF_KUB_PATH  varchar2 1000 byte

这是我在PL / SQL中的程序,我无法找到我的错误,提前谢谢

CREATE OR REPLACE PROCEDURE Deneme
IS
    BARCODETEMP VARCHAR2(13 BYTE);
    S_KUB VARCHAR2 (1000 BYTE);

    CURSOR c1 IS
    SELECT a.PDF_KUB_PATH, a.BARCODE
    FROM medicine b,parsed_ilac_rehberi a
    WHERE a.BARCODE = b.barcode;
BEGIN

    OPEN c1;
    LOOP
    FETCH c1 INTO  S_KUB , BARCODETEMP,

    EXIT WHEN c1%NOTFOUND;

    UPDATE medicine
    SET PDF_KUB_PATH = S_KUB
    WHERE BARCODE = BARCODETEMP; 

    END LOOP;

    CLOSE c1;

    COMMIT;

EXCEPTION
    WHEN OTHERS THEN
    RAISE_APPLICATION_ERROR(-20001,'An error was encountered - '
                                    ||SQLCODE||' -ERROR'||SQLERRM);
end;

3 个答案:

答案 0 :(得分:0)

我认为在此行编译时会抛出警告/错误

FETCH c1 INTO  S_KUB , BARCODETEMP,

它应该是(以冒号而不是逗号结尾)

FETCH c1 INTO  S_KUB , BARCODETEMP;

您可以使用MERGE

来避开光标并执行此操作
CREATE OR REPLACE PROCEDURE Deneme
IS
    MERGE INTO medicine m
    USING
    (
      SELECT a.PDF_KUB_PATH, a.BARCODE FROM parsed_ilac_rehberi a
    ) v
    ON (m.BARCODE = v.BARCODE)
    WHEN MATCHED THEN
    UPDATE SET m.PDF_KUB_PATH = v.PDF_KUB_PATH

    COMMIT;

    EXCEPTION
        WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20001,'An error was encountered - '
                                    ||SQLCODE||' -ERROR'||SQLERRM);
END;

答案 1 :(得分:0)

更短版本(1插入不需要MERGE),您不需要重新定义异常(如Ben所述)

CREATE OR REPLACE PROCEDURE Deneme
IS
UPDATE (select a.PDF_KUB_PATH src, m.PDF_KUB_PATH dst
FROM medicine m INNER JOIN parsed_ilac_rehberi a
ON m.BARCODE = a.BARCODE)
SET dst = src;    
END;

答案 2 :(得分:0)

您不需要使用过程,只需使用sql脚本就可以解决您的问题。

update medicine m
   set   m.PDF_KUB_PATH = (select p.PDF_KUB_PATH from parsed_ilac_rehberi p where m.BARCODE = p.BARCODE);