Oracle Select From主键列是否返回null值?

时间:2014-08-28 02:39:37

标签: oracle

我有一个从查询创建的视图。此查询从连接在一起的2个表中检索数据。其中一个使用主键进行内连接。但是数据在主键上返回null结果。这有什么问题?

CREATE TABLE orderinfo
(
    orderid   VARCHAR2 (20),
    qtty      NUMBER (20, 0),
    price     NUMBER (20, 0)
)
/

ALTER TABLE orderinfo ADD CONSTRAINT orderinfo_pk PRIMARY KEY (orderid)
/
INSERT INTO orderinfo (orderid, qtty, price)
  VALUES   ('OD00001', 1000, 15000);

INSERT INTO orderinfo (orderid, qtty, price)
  VALUES   ('OD00002', 2000, 15000);

INSERT INTO orderinfo (orderid, qtty, price)
  VALUES   ('OD00003', 3000, 15000);

INSERT INTO orderinfo (orderid, qtty, price)
  VALUES   ('OD00004', 4000, 15000);

COMMIT
/

create table ordersend
(
    orderid varchar2(20),
    orderstatus varchar2(3)
)
ALTER TABLE ordersend ADD CONSTRAINT ordersend_pk PRIMARY KEY (orderid)
/

INSERT INTO ordersend (orderid, status)
  VALUES   ('OD00001', 'N');

INSERT INTO ordersend (orderid, status)
  VALUES   ('OD00002', 'N');

INSERT INTO ordersend (orderid, status)
  VALUES   ('OD00003', 'N');

INSERT INTO ordersend (orderid, status)
  VALUES   ('OD00004', 'N');

COMMIT
/

CREATE VIEW vw_ordersendstatus
AS
    SELECT   "orderid",
             "qtty",
             "price",
             "status"
      FROM   (SELECT   inf.orderid,
                       inf.qtty,
                       inf.price,
                       send.status
                FROM   orderinfo inf, ordersend send
               WHERE   inf.orderid = send.orderid AND status = 'N')
/
CREATE TABLE orderblock
(
    orderid   VARCHAR2 (20),
    qtty      NUMBER (20, 0),
    price     NUMBER (20, 0)
)
/
CREATE PROCEDURE pr_sendorder
IS
    CURSOR c_send
    IS
        SELECT   orderid orderid,
                 qtty,
                 price,
                 status
          FROM   vw_ordersendstatus;
BEGIN
    FOR i IN c_send
    LOOP
        INSERT INTO orderblock (orderid, qtty, price)
          VALUES   (i.orderid, i.qtty, i.price);

        UPDATE   ordersend
           SET   status = 'B'
         WHERE   orderid = i.orderid;
    END LOOP;
EXCEPTION
    WHEN OTHERS
    THEN
        RAISE '-1';
END;

然后 SELECT * FROM orderblock

在orderid字段

上返回null值

为什么??? 请帮忙

2 个答案:

答案 0 :(得分:0)

ordersend的create table不包含视图中提到的STATUS列。

在视图中,您使用了引用的小写列名称。引号强制Oracle将它们视为区分大小写。

如果混淆引用的和不带引号的列/变量名称,您可能会遇到一个情况,即未引用的orderid从大写的ORDERID或未引用的orderid中获取其值,而不是引用的小写" orderid"你期待的。最好避免使用列,变量和表的带引号标识符。

如果没有完整的脚本,很难确定

答案 1 :(得分:0)

谢谢加里, 在我的问题中,我有一个错误AT视图。 正确的是:

INSERT INTO ordersend (orderid, orderstatus)
  VALUES   ('OD00001', 'N');

INSERT INTO ordersend (orderid, orderstatus)
  VALUES   ('OD00002', 'N');

INSERT INTO ordersend (orderid, orderstatus)
  VALUES   ('OD00003', 'N');

INSERT INTO ordersend (orderid, orderstatus)
  VALUES   ('OD00004', 'N');

CREATE VIEW vw_ordersendstatus
AS
    SELECT   "ORDERID",
             "QTTY",
             "PRICE",
             "STATUS"
      FROM   (SELECT   inf.orderid,
                       inf.qtty,
                       inf.price,
                       send.orderstatus status
                FROM   orderinfo inf, ordersend send
               WHERE   inf.orderid = send.orderid AND orderstatus = 'N')