Following is the scenario:
CREATE TABLE TEST1
(
ITEM VARCHAR2 (12),
EQ_TYPE VARCHAR2 (4),
PRIMARY KEY (ITEM)
);
CREATE TABLE TEST2
(
ITEM VARCHAR2 (12),
JOBNUM VARCHAR2 (12),
EQ_GROUP_TYPE VARCHAR2 (20),
PRIMARY KEY (ITEM, JOBNUM)
);
INSERT INTO TEST1 (ITEM, EQ_TYPE) VALUES ('AAAA1', '1AES');
INSERT INTO TEST1 VALUES ('BBBB1', '5EXM');
INSERT INTO TEST1 VALUES ('CCCC1', 'D100');
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX1', NULL);
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX2', NULL);
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX3', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY1', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY2', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY3', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY4', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY5', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY6', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY7', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ1', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ2', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ3', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ4', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ5', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ6', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ7', NULL);
Following is the cursor code I wrote to update table TEST2.EQ_GROUP_TYPE based on case statement and join between TEST1 & TEST2 based on ITEM. But I am getting an error: ORA-01427: single-row subquery returns more than one row
ORA-06512: at line 19
Now, I know its returning more than one row...i just do not know how to fix it? Any help will be highly appreciated. In reality TEST1 & TEST2 contains lot more rows and this case statement has more variations.
DECLARE
CURSOR cur IS
SELECT A.ITEM
FROM TEST2 A, TEST1 B
WHERE A.ITEM = B.ITEM;
CURRENT_VALUE VARCHAR2(20);
to_commit NUMBER :=0;
BEGIN
OPEN cur;
FETCH cur INTO CURRENT_VALUE;
WHILE cur%FOUND
LOOP
UPDATE TEST2
SET EQ_GROUP_TYPE =
(SELECT CASE
WHEN B.EQ_TYPE = '1AES' THEN '1AE'
WHEN B.EQ_TYPE = '5EXM' THEN '5ES'
WHEN B.EQ_TYPE = 'D100' THEN 'DMS'
ELSE NULL
END
FROM TEST1 B, TEST2 A
WHERE (B.ITEM = A.ITEM ) AND (A.ITEM=CURRENT_VALUE) );
IF to_commit >= 1000
THEN
COMMIT;
to_commit := 0;
END IF;
to_commit := to_commit + 1;
FETCH cur INTO CURRENT_VALUE;
END LOOP;
COMMIT;
CLOSE cur;
END;
CREATE TABLE TEST1
(
ITEM VARCHAR2 (12),
EQ_TYPE VARCHAR2 (4),
PRIMARY KEY (ITEM)
);
CREATE TABLE TEST2
(
ITEM VARCHAR2 (12),
JOBNUM VARCHAR2 (12),
EQ_GROUP_TYPE VARCHAR2 (20),
PRIMARY KEY (ITEM, JOBNUM)
);
INSERT INTO TEST1 (ITEM, EQ_TYPE) VALUES ('AAAA1', '1AES');
INSERT INTO TEST1 VALUES ('BBBB1', '5EXM');
INSERT INTO TEST1 VALUES ('CCCC1', 'D100');
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX1', NULL);
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX2', NULL);
INSERT INTO TEST2 VALUES ('AAAA1', 'XXXX3', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY1', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY2', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY3', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY4', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY5', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY6', NULL);
INSERT INTO TEST2 VALUES ('BBBB1', 'YYYY7', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ1', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ2', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ3', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ4', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ5', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ6', NULL);
INSERT INTO TEST2 VALUES ('CCCC1', 'ZZZZ7', NULL);
Following is the cursor code I wrote to update table TEST2.EQ_GROUP_TYPE based on case statement and join between TEST1 & TEST2 based on ITEM. But I am getting an error: ORA-01427: single-row subquery returns more than one row
ORA-06512: at line 19
Now, I know its returning more than one row...i just do not know how to fix it? Any help will be highly appreciated. In reality TEST1 & TEST2 contains lot more rows and this case statement has more variations.
DECLARE
CURSOR cur IS
SELECT A.ITEM
FROM TEST2 A, TEST1 B
WHERE A.ITEM = B.ITEM;
CURRENT_VALUE VARCHAR2(20);
to_commit NUMBER :=0;
BEGIN
OPEN cur;
FETCH cur INTO CURRENT_VALUE;
WHILE cur%FOUND
LOOP
UPDATE TEST2
SET EQ_GROUP_TYPE =
(SELECT CASE
WHEN B.EQ_TYPE = '1AES' THEN '1AE'
WHEN B.EQ_TYPE = '5EXM' THEN '5ES'
WHEN B.EQ_TYPE = 'D100' THEN 'DMS'
ELSE NULL
END
FROM TEST1 B, TEST2 A
WHERE (B.ITEM = A.ITEM ) AND (A.ITEM=CURRENT_VALUE) );
IF to_commit >= 1000
THEN
COMMIT;
to_commit := 0;
END IF;
to_commit := to_commit + 1;
FETCH cur INTO CURRENT_VALUE;
END LOOP;
COMMIT;
CLOSE cur;
END;