我有三个表格Issue
,IssueReports
和Location
,结构如下。
问题和位置具有多对一关系。 Issue和IssueReports没有物理链接,但可以有一些共同的字段。
IssueReports
-------------
ID (Number);
Issue_Number (Varchar);
Issue_Loc_Name (Varchar)
and some other fields for the report....
Issue
-----------------
ID (Number);
IssueNumber (Varchar);
Issue_Loc_Id (Number)
Location
-----------------
ID (Number);
Loc_Name (Varchar)
我想更新Issue_Loc_Name
多行中的IssueReports
字段Loc_Name
IssueNumber
问题表和IssueNumber
IssueReport
表是平等的。
我已经编写了以下过程来根据上面给出的条件更新多个记录,但是我获得的是表Issue_Loc_name
的多行IssueReport
的相同值。
DECLARE
IR_LOCNAME VARCHAR2(10);
IRNUM_TO_SEARCH VARCHAR2(10);
ISNUM_TOSEARCH VARCHAR2(10);
Cursor c1 is select ir.Issue_Number, is.IssueNumber, loc.loc_name
from IssueReports ir, Issue is, Location loc
where loc.id = is.Issue_Loc_Id
AND ir.Issue_Number = is.IssueNumber;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO IRNUM_TO_SEARCH, ISNUM_TOSEARCH, IR_LOCNAME;
DBMS_OUTPUT.put_line(IRNUM_TO_SEARCH || ' ' || ISNUM_TOSEARCH || ' ' || IR_LOCNAME);
EXIT WHEN c1%NOTFOUND;
UPDATE IssueReports SET Issue_Loc_Name = IR_LOCNAME;
WHERE IRNUM_TO_SEARCH = ISNUM_TOSEARCH;
END LOOP;
CLOSE C1;
END;
/
我的光标有问题吗?我正在使用Oracle 11G。
最诚挚的问候, MouliPC。
答案 0 :(得分:1)
我认为问题在于:
UPDATE IssueReports SET Issue_Loc_Name = IR_LOCNAME;
WHERE IRNUM_TO_SEARCH = ISNUM_TOSEARCH;
如果您的两个局部变量IRNUM_TO_SEARCH
和ISNUM_TO_SEARCH
恰好相同,则会更新整个表格。否则,不会更新任何行。
你或许打算写以下内容吗?
UPDATE IssueReports SET Issue_Loc_Name = IR_LOCNAME;
WHERE IssueNumber = ISNUM_TOSEARCH;
答案 1 :(得分:0)
我可以使用MERGE通过以下查询完成更新。
MERGE INTO IssueReport IR using
(select ir.Issue_Number as irin,
is.IssueNumber as isin,
loc.loc_name as location
from IssueReports ir,
Issue is,
Location loc
where loc.id = is.Issue_Loc_Id AND ir.Issue_Number = is.IssueNumber) result
ON (is.issue_number = result.isin)
WHEN MATCHED then update set Issue_Loc_Name = result.location;