Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, GAJANANA.

Asked: March 17, 2017 - 5:50 am UTC

Last updated: March 19, 2017 - 5:12 am UTC

Version: 11.1.0.6.0

Viewed 1000+ times

You Asked

Hi Tom,

I have table called customers with data below

SQL> select * from customers;

ID NAME AGE ADDRESS SALARY
---------- -------------------- ---------- -------------------------------------------------- ----------
1 Ramesh 35 Ahamedabad 2000
2 Khilan 25 Delhi 1500
3 Kaushik 23 Kota 2000
4 Chaithali 25 Mumbai 6500
5 Hardik 27 Bhoopal 8500
6 Kamal 22 MadhyaPradesh 4500
7 Muffy 24 Indore 10000
8 Md Rafi 31 UttarPradesh 1500
9 Gajanana 30 karnataka 45000

No i need to increment salary of all the employees

code here


SQL> declare
2 type aat is table of number index by pls_integer;
3 vtemp aat;
4 begin
5 select *
6 bulk collect into vtemp
7 from customers;
8 forall i in 1..vtemp.last
9 update customers set salary=salary+200
10 where salary=vtemp(i);
11 end;
12 /
from customers;
*
ERROR at line 7:
ORA-06550: line 6, column 25:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 5, column 1:
PL/SQL: SQL Statement ignored


Need fix for this. Your help will be appreciated.

and Chris said...

Why are you using forall for this? Why not just:

update customers set salary=salary+200


?

Anyway, the problem comes because you're trying to select all the columns into a number. You need to change your type declaration to be a table of customers%rowtype:

type aat is table of customers%rowtype index by pls_integer;


Or, better, make a record type with just the columns you need. Then only select those.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

bulk update

GAJANANA GANJIGATTI, March 17, 2017 - 3:57 pm UTC

Hi Tom,

As you said i have done it. even though getting error

declare
2 type aat is table of customers%rowtype index by pls_integer;
3
4 vtemp aat;
5 begin
6 select *
7 bulk collect into vtemp
8 from customers;
9 forall i in 1..vtemp.last
10 update customers set salary=salary+200
11 where salary=vtemp(i);
12 end;
13 /
where salary=vtemp(i);
*
ERROR at line 11:
ORA-06550: line 11, column 14:
PLS-00382: expression is of wrong typ
Chris Saxon
March 17, 2017 - 5:51 pm UTC

You've just moved the error around!

You need to set salary to the salary component of vtemp

vtemp(i).salary


But seriously, ditch the forall and just update!

Davide Golinelli, March 17, 2017 - 4:50 pm UTC

GAJANANA you misunderstood the response, the error in your code is here
salary=vtemp(i);

salary is a number and vtemp(i) a record with different fields

anyway your code make no sense to me :-D
Chris Saxon
March 17, 2017 - 5:53 pm UTC

The original error was in the select (PL/SQL: ORA-00947: not enough values). So fixing this just moved the problem around!

But yes, the code is a little bizarre...

bulk update

GAJANANA GANJIGATTI, March 18, 2017 - 5:33 am UTC

Thank you very much for me to understanding the concept. thanks alot Chris.
Connor McDonald
March 19, 2017 - 5:12 am UTC

glad we could help