SQL插入值sequence.NEXTVAL

时间:2016-03-06 01:48:02

标签: sql oracle plsql sqlplus sequences

相对较新的pl / sql和我试图找到一种方法在我的循环中使用sequence.NEXTVAL。当我运行我现有的东西时,我得到" ORA-01722:无效的号码"我有时会失踪吗?

DECLARE 
  CURSOR SALESDATA
  IS
    SELECT vin,
           sale_date,
           SF.plan_ID,
           SP.salepersons_ID,
           COUNT (*) AS Vehicles_Sold,
           SUM (S.gross_sales_price) AS Gross_Sales_Amt
    FROM sales S, sales_financing SF, salepersons SP
    WHERE S.sale_ID = SF.sale_ID AND S.salepersons_ID = SP.salepersons_ID
   GROUP BY vin,
          sale_date,
          SF.plan_ID,
          SP.salepersons_ID
     HAVING COUNT (*) > 0;
  BEGIN
    FOR VEHICLE_REC IN SALESDATA
    LOOP
      INSERT INTO sale_facts (sale_fact_ID,
                              sale_day,
                              vehicle_code,
                              plan_code,
                              dealer_ID,
                              vehicle_sold,
                              gross_sales_amt)
          VALUES (salefactID_seq.NEXTVAL,
                  vehicle_rec.sale_date,
                  vehicle_rec.vin,
                  vehicle_rec.plan_id,
                  vehicle_rec.salepersons_ID,
                  vehicle_rec.vehicles_sold,
                  vehicle_rec.gross_sales_amt);

     COMMIT;
    END LOOP;

    COMMIT;
  END;
 /

这里有桌子'结构。

SQL> DESC sales
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SALE_ID                                   NOT NULL NUMBER(10)
 VIN                                                VARCHAR2(17)
 CUST_ID                                            NUMBER(10)
 GROSS_SALES_PRICE                         NOT NULL NUMBER(10)
 MILEAGE                                   NOT NULL VARCHAR2(10 CHAR)
 SALE_DATE                                 NOT NULL DATE
 VEHICLE_STATUS                            NOT NULL VARCHAR2(25 CHAR)
 SALEPERSONS_ID                                     NUMBER(10)

SQL> DESC salepersons
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SALEPERSONS_ID                            NOT NULL NUMBER(10)
 TITLE                                     NOT NULL VARCHAR2(25 CHAR)
 NAME                                      NOT NULL VARCHAR2(50 CHAR)
 HIRE_DATE                                 NOT NULL DATE

SQL> DESC sales_financing
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SALE_ID                                            NUMBER(10)
 PLAN_ID                                            NUMBER(10)
 DOWN_PAY                                  NOT NULL FLOAT(10)
 LOAN_TERM                                 NOT NULL NUMBER(3)

SQL> DESC sale_facts
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 SALE_FACT_ID                              NOT NULL NUMBER(5)
 SALE_DAY                                  NOT NULL DATE
 VEHICLE_CODE                              NOT NULL NUMBER(5)
 PLAN_CODE                                 NOT NULL NUMBER(10)
 DEALER_ID                                 NOT NULL NUMBER(10)
 VEHICLE_SOLD                              NOT NULL NUMBER(10)
 GROSS_SALES_AMT                           NOT NULL NUMBER(15)

1 个答案:

答案 0 :(得分:1)

您的' vehicle_code'在' sale_facts'是NUMBER(5),而' VIN'在'销售'是VARCHAR2(17)。您确定VIN的所有值都可以转换为NUMBER(5)吗?即,那里没有人物?