如何从另一个查询结果中减去?

时间:2019-12-15 23:20:52

标签: sql oracle

该过程可以正常编译,但是当我传入用户名(用户名也有效)时,出现以下错误,我知道减法没有返回值,但是为什么呢?这是错误:

ORA-06512: at "KOBS24.ADD_PAYMENT", line 5
01400. 00000 -  "cannot insert NULL into (%s)"
*Cause:    An attempt was made to insert NULL into previously listed objects.
*Action:   These objects cannot accept NULL values.```

这是我的代码:

create or replace PROCEDURE add_payment(username2 VARCHAR2)
IS
BEGIN

INSERT INTO user_pay VALUES(
    user_pay_seq.nextval,
        (SELECT offer_price FROM users_pending WHERE username = username2),
            ((SELECT veh_price FROM vehicle_table WHERE lot_id = (SELECT pend_lot_id FROM users_pending WHERE username = username2)) - 
                (SELECT offer_price FROM users_pending WHERE username = username2)),
                    (SELECT pay_id FROM USERS_TABLE WHERE lot_id = 
                    (SELECT lot_id FROM VEHICLE_TABLE WHERE lot_id =
                        (SELECT pay_id FROM users_pending WHERE username = username2)))
);
UPDATE VEHICLE_TABLE
SET
   veh_status = 'owned',
    owner_user = username2
    WHERE (SELECT username FROM users_pending) = username2;
END;

1 个答案:

答案 0 :(得分:0)

很难从您的查询中分辨出来,但是我对您尝试做的最好的解释是

SELECT NVL(up.OFFER_PRICE, 0),
       NVL(vt.VEH_PRICE, 0) - NVL(up.OFFER_PRICE, 0),
       NVL(ut.PAY_ID, 0)
  FROM USERS_PENDING up
  INNER JOIN VEHICLE_TABLE vt
    ON vt.PEND_LOT_ID = up.LOT_ID
  INNER JOIN USERS_TABLE ut
    ON ut.LOT_ID = vt.LOT_ID
  WHERE up.USERNAME = username2

如果没有测试数据或预期结果无法奏效,我不能太自信这是正确的,但是根据您发布的内容,这是我能做的最好的事情。