PL/SQL Block statement to populate table

时间:2018-03-25 18:42:39

标签: oracle plsql concatenation sqlplus

I am very new to PL/SQL and this one has me stumped. In SQLPLUS, I need to "Use a PL/SQL block to populate the Description column by selecting the combinations from your OLTP_VEHICLES table and then inserting the combinations into your new VEHICLES table, which would best be performed via a cursor in a loop." This is what I have:

CREATE TABLE OLTP_Vehicles (
VIN VARCHAR(20) NOT NULL,
trade_ID VARCHAR(10) NOT NULL,
type VARCHAR(10),
make VARCHAR(15),
model VARCHAR(15),
where_from VARCHAR(30),
wholesale_cost NUMBER(8,2) NOT NULL,
PRIMARY KEY (VIN),
CONSTRAINT UC_Sale_Vehicles UNIQUE (VIN,trade_ID));

CREATE TABLE Vehicles (
vehicle_Code VARCHAR2(10),
description VARCHAR2(100),
PRIMARY KEY (vehicle_Code));

CREATE SEQUENCE veh_code_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;

--PL/SQL Block
**SET SERVEROUTPUT ON
BEGIN
    FOR i in 1..10
    LOOP
        INSERT INTO Vehicles (vehicle_Code, description)
        VALUES veh_code_seq.NEXTVAL, (SELECT CONCAT(make, ', ',model) AS 
        description FROM OLTP_Vehicles); 
    END LOOP;
END;
/**

But I am getting this error:

VALUES veh_code_seq.NEXTVAL, (SELECT CONCAT(make, ', ',model) AS description FROM OLTP_Vehicles); * ERROR at line 5: ORA-06550: line 4, column 50: PL/SQL: ORA-00947: not enough values ORA-06550: line 4, column 1: PL/SQL: SQL Statement ignored

I am assuming this is my SELECT CONCAT statement that is causing the issue. How can I pull in two columns from another table and insert them as one column in a new table?

MODIFIED Statement

SET SERVEROUTPUT ON
DECLARE
CURSOR c_VIN IS SELECT DISTINCT VIN FROM OLTP_Vehicles;
BEGIN
    FOR c1 in c_VIN
    LOOP
        INSERT INTO Vehicles (vehicle_Code, description)
        VALUES (veh_code_seq.NEXTVAL, (SELECT make || ', '||model AS 
        description
        FROM OLTP_Vehicles 
        WHERE VIN = c1.VIN));
    END LOOP;
END;
/

RESULTS:

SQL> SELECT * FROM Vehicles ORDER BY vehicle_Code DESC;

VEHICLE_CODE    DESCRIPTION
--------------- ------------------------------
9               Chevrolet, Camaro
8               Chevrolet, Colorado
7               Dodge, Challenger
6               Ford, Fusion
50              Ford, Taurus
5               Chevrolet, Silverado
49              Chevrolet, Tahoe
48              Chevrolet, Colorado
47              Chevrolet, Colorado
46              Chevrolet, Silverado
45              Dodge, Ram

VEHICLE_CODE    DESCRIPTION
--------------- ------------------------------
44              Chevrolet, Impala
43              Chevrolet, Silverado
42              Chevrolet, Silverado
41              Chevrolet, Silverado
40              Chevrolet, Malibu
4               Ford, Focus
39              Dodge, Ram
38              Chevrolet, Camaro
37              Ford, F-350
36              Dodge, Ram
35              Ford, Fiesta

VEHICLE_CODE    DESCRIPTION
--------------- ------------------------------
34              Dodge, Dakota
33              Ford, F-150
32              Chevrolet, Silverado
31              Chevrolet, Suburban
30              Ford, Expedition
3               Dodge, Durango
29              Chevrolet, Colorado
28              Chevrolet, Tahoe
27              Dodge, Ram
26              Chevrolet, Silverado
25              Dodge, Charger

VEHICLE_CODE    DESCRIPTION
--------------- ------------------------------
24              Chevrolet, Silverado
23              Ford, F-150
22              Chevrolet, Tahoe
21              Chevrolet, Suburban
20              Ford, Expedition
2               Ford, Focus
19              Dodge, Charger
18              Chevrolet, Corvette
17              Chevrolet, Tahoe
16              Chevrolet, Suburban
15              Ford, F-250

VEHICLE_CODE    DESCRIPTION
--------------- ------------------------------
14              Ford, Edge
13              Chevrolet, Colorado
12              Chevrolet, Corvette
11              Dodge, Charger
10              Chevrolet, Camaro
1               Ford, Fusion

50 rows selected.

SQL>

Now I need to figure out how to get the distinct make and model as they do not connect with the VIN and I need to figure out why the vehicle_Code is not starting at 1 and DESC order.

3 个答案:

答案 0 :(得分:1)

The concat function only takes two parameters, not three. Just replace with the string concatenation operator || Example:

BEGIN
    FOR i in 1..10
    LOOP
        INSERT INTO Vehicles (vehicle_Code, description)
        VALUES (veh_code_seq.NEXTVAL, (SELECT make||', '||model AS 
        description FROM OLTP_Vehicles) ); 
    END LOOP;
END;

However, that is not the end of your problems. Your embedded SELECT statement does not make sense. It is reading all the rows from OLTP_Vehicles on each loop iteration. Was that your intent?

答案 1 :(得分:0)

Try below block code,

Declare
cursor c_vin is select distinct vin /*unique id*/ from OLTP_Vehicles;

begin

for c1 in c_vin loop

INSERT INTO Vehicles
(vehicle_Code, description)
SELECT veh_code_seq.NEXTVAL, make ||', '|| model
FROM OLTP_Vehicles where vin=c1.vin;


End loop;
commit;

End;

答案 2 :(得分:0)

我认为您需要做的就是使用以下查询将数据插入到Vehicle表中如果是一次性工作。

insert into Vehicles 
select  rownum,  description from ( select  distinct initcap(trim(make)) ||', '||initcap(trim(model)) description from OLTP_Vehicles) ;