无法从SQL Plus中的主表的连接信息填充辅助表

时间:2019-07-13 17:55:33

标签: oracle plsql

我有一个作业,我要为以后的作业进行更正: 对于Vehicle_Code主键列,请使用Oracle序列填充值。对于“描述”列,请使用您拥有的车辆的品牌和型号的所有串联组合。通过从OLTP_VEHICLES表中选择组合,然后将组合插入到新的VEHICLES表中,使用PL / SQL块填充“描述”列,最好通过循环中的游标执行此操作。 现在,我以前这样做过,但是针对错误的表。我应该创建一个新表来将该信息插入其中。我创建了新表并进行了我认为适当的更正,但是新表始终返回0行。以下是我的原始代码和更新的代码。有人可以告诉我我在想什么吗?谢谢!

原始代码:

DROP SEQUENCE OLTP_Counter;
CREATE SEQUENCE OLTP_Counter START WITH 1
INCREMENT BY 1
MINVALUE 1;

DECLARE
CURSOR OLTP_COUNT IS SELECT vehicle_code,make, model FROM OLTP_VEHICLES;
vc varchar2(30);
model VARCHAR2(30);
make VARCHAR2(30);
BEGIN
OPEN OLTP_COUNT;
LOOP
FETCH OLTP_COUNT INTO vc, make, model;
EXIT WHEN  OLTP_COUNT % NOTFOUND;
UPDATE OLTP_VEHICLES SET description=make||' '||model where vehicle_code=vc;
END LOOP;
CLOSE OLTP_COUNT;
COMMIT;
END;
/
SELECT * FROM OLTP_VEHICLES ORDER BY vehicle_code;

新(残破)代码:

CREATE SEQUENCE vehicle_counter START WITH 1
INCREMENT BY 1
MINVALUE 1;

DECLARE
CURSOR vehicle_count IS SELECT vehicle_code,make, model FROM OLTP_VEHICLES;
vc VARCHAR2(30);
model VARCHAR2(30);
make VARCHAR2(30);
BEGIN
OPEN vehicle_count;
LOOP
FETCH vehicle_count INTO vc, make, model;
EXIT WHEN  vehicle_count % NOTFOUND;
UPDATE vehicles SET description=make||' '||model where Vehicle_Code=vc;
END LOOP;
CLOSE vehicle_count;
COMMIT;
END;
/

SELECT * FROM vehicles ORDER BY Vehicle_Code;

此新代码实际上并未填充Vehicles表。它返回“未选择行”

添加的信息

下面有OLTP_Vehicles和车辆的表格。我的可变长度应该足够了。

CREATE TABLE OLTP_VEHICLES (
    Vehicle_Code VARCHAR2(10),
    CONSTRAINT OLTPV_pk PRIMARY KEY(Vehicle_Code),
    vin VARCHAR2(17),
    type VARCHAR2(20) NOT NULL,
    model VARCHAR2(20) NOT NULL,
    make VARCHAR2(20) NOT NULL,
    trade_id VARCHAR2 (10)NOT NULL, 
    wholesale_cost NUMERIC(10,2) NOT NULL,
    where_from VARCHAR2(30)NOT NULL,
    sale_id VARCHAR2(10),
    dealer_id VARCHAR2(10)
    );

CREATE TABLE vehicles (
    Vehicle_Code VARCHAR2(10),
    CONSTRAINT vehicles_pk PRIMARY KEY(Vehicle_Code),
    description VARCHAR2(65)
    );

1 个答案:

答案 0 :(得分:0)

知道了!在我更新之前,已经存在的记录。新表需要一个INSERT。感谢所有给我建议的人。作为仍在学习的人,这些东西都不是自然而然的。我需要我能获得的所有帮助!

DECLARE
CURSOR vehicle_counter IS SELECT Vehicle_Code, make, model FROM OLTP_VEHICLES;
vc VARCHAR2(10);
model OLTP_Vehicles.model%type;
make OLTP_Vehicles.make%type;
BEGIN
OPEN vehicle_counter;
LOOP
FETCH vehicle_counter INTO vc,make, model;
dbms_output.put_line(make);
EXIT WHEN  vehicle_counter % NOTFOUND;
INSERT INTO vehicles values(vc,make||' '||model);
END LOOP;
CLOSE vehicle_counter;
COMMIT;
EXCEPTION WHEN others THEN dbms_output.put_line(sqlerrm);
END;
/