Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Single Query to Assign Multiple Scalar Variables

IniyavanOct 19 2017 — edited Oct 19 2017

Hi friends,

I have data like this in EMP table:

Emp_Id    Last_Name    Dept_Id      Salary

------------------------------------------------------------

101          Arun                 10               1000

102          Bala                 20               2000

103          Arun                 20               3000

104          Bala                 30               4000

In PL/SQL, I need to assign Salaries of these 4 employees in 4 different local scalar variable in a single query. Something like this:

DECLARE

V_SAL_OF_101 INTEGER;

V_SAL_OF_102 INTEGER;

V_SAL_OF_103 INTEGER;

V_SAL_OF_104 INTEGER;

BEGIN

  --Forget about primary key, or other constraints.

  select salary

  into V_SAL_OF_101

  from EMP

  where emp_id = 101

  and last_name = 'Arun';

  select salary

  into V_SAL_OF_102

  from EMP

  where emp_id = 102

  and last_name = 'Bala';

  --An so on.

  --Instead of querying multiple times, is it possible to assign these scalar variables in a single query (without using a collection).

END;

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 16 2017
Added on Oct 19 2017
7 comments
1,304 views