PL/SQL:我在使用另一个 table 的数据更新 table 时遇到问题
PL/SQL: I have problem updating a table with data from another table
第一个tableROOMDB
:
roomnumber
rentalbalance
N327
0
第二个tableRENTALINVOICE
:
invoicedate
roomnumber
totaldue
11/26/2021
N327
2,200.00
我的更新码:
UPDATE ROOMDB
SET
RENTALBALANCE = (SELECT TOTALDUE
FROM RENTALINVOICE
WHERE RENTALINVOICE.ROOMNUMBER=ROOMDB.ROOMNUMBER
AND INVOICEDATE=SYSDATE) ;
我需要用 RENTALINVOICE
中的数据更新 ROOMDB
中的 totaldue
列,尽管它成功地将 2,200 输入到 totaldue
列,同时它也在 ROOMDB
.
中清除此列上的其余记录
每次我更新它时,它都会删除除我指定的 roomnumber
之外的其余记录。请帮助。
您基本上似乎还更新了 table roomdb
中的行,其中 table rentalinvoice
中没有行,因此 rentalbalance
列将是设置为空。
看看下面的例子:
drop table a;
create table a (id number, cost number);
insert into a values (1, 1);
insert into a values (2, 2);
drop table b;
create table b (id number, cost number);
insert into b values (1, 100);
-- updates all rows in a and sets cost to null whenen there is no row in b
update a set cost = (select cost from b where a.id = b.id);
select * from a;
-- only updaes rows in a where there is a row in b
update a set cost = id;
update a set cost = (select cost from b where a.id = b.id) where exists (select 1 from b where a.id = b.id);
select * from a;
当您执行这样的更新命令时,Oracle 将更新所有 table 行。对于每一行 Select 命令将使用这些行值执行。如果更新的结果select没有找到任何记录,则return为null。在这种情况下,RENTALBALANCE
列也将设置为空。
(SELECT TOTALDUE
FROM RENTALINVOICE
where WHERE RENTALINVOICE.ROOMNUMBER = ROOMDB.ROOMNUMBER
AND INVOICEDATE = SYSDATE)
第一个tableROOMDB
:
roomnumber | rentalbalance |
---|---|
N327 | 0 |
第二个tableRENTALINVOICE
:
invoicedate | roomnumber | totaldue |
---|---|---|
11/26/2021 | N327 | 2,200.00 |
我的更新码:
UPDATE ROOMDB
SET
RENTALBALANCE = (SELECT TOTALDUE
FROM RENTALINVOICE
WHERE RENTALINVOICE.ROOMNUMBER=ROOMDB.ROOMNUMBER
AND INVOICEDATE=SYSDATE) ;
我需要用 RENTALINVOICE
中的数据更新 ROOMDB
中的 totaldue
列,尽管它成功地将 2,200 输入到 totaldue
列,同时它也在 ROOMDB
.
每次我更新它时,它都会删除除我指定的 roomnumber
之外的其余记录。请帮助。
您基本上似乎还更新了 table roomdb
中的行,其中 table rentalinvoice
中没有行,因此 rentalbalance
列将是设置为空。
看看下面的例子:
drop table a;
create table a (id number, cost number);
insert into a values (1, 1);
insert into a values (2, 2);
drop table b;
create table b (id number, cost number);
insert into b values (1, 100);
-- updates all rows in a and sets cost to null whenen there is no row in b
update a set cost = (select cost from b where a.id = b.id);
select * from a;
-- only updaes rows in a where there is a row in b
update a set cost = id;
update a set cost = (select cost from b where a.id = b.id) where exists (select 1 from b where a.id = b.id);
select * from a;
当您执行这样的更新命令时,Oracle 将更新所有 table 行。对于每一行 Select 命令将使用这些行值执行。如果更新的结果select没有找到任何记录,则return为null。在这种情况下,RENTALBALANCE
列也将设置为空。
(SELECT TOTALDUE
FROM RENTALINVOICE
where WHERE RENTALINVOICE.ROOMNUMBER = ROOMDB.ROOMNUMBER
AND INVOICEDATE = SYSDATE)