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!

Dynamic SQL Attribute Name

User_5T7BPMay 17 2019 — edited May 20 2019

I have employee data like this, this is a sample table as I have 300+ columns in actual table

  1. create table EMPLOYEE(EMPID number,FST_NAME varchar2(100),LAST_NAME varchar2(100),EMAIL varchar2(100),addr1 varchar2(100));

  2. insert into EMPLOYEE values (123,'Jack','John','jj@gmail.com','PO 200');

  3. insert into EMPLOYEE values (124,'Mary','Sony','8j@gmail.com','PO 200');

pastedImage_0.png

I have the ATTRIBUTE table, where I assigned a key to some of the attributes in EMPLOYEE table not in same order

create table ATTRIBUTE(KEY number, ATT_NAME varchar2(100));

insert into ATTRIBUTE values(1,'FST_NAME');

insert into ATTRIBUTE values(2,'EMAIL');

insert into ATTRIBUTE values(3'LAST_NAME');

pastedImage_3.png

I have the RESULT table which got the employees which are split attribute wise

create table RESULT(KEY number, EMPID number, DATA_VAL varchar2(100));

INSERT /*+ APPEND */ INTO RESULT (EMPID,KEY)

(select H.EMPID,R.KEY from EMPLOYEE H,ATTRIBUTE R

);

commit;

pastedImage_4.png

My requirement is to take the corresponding attribute value from EMPLOYEE table based on the key in ATTRIBUTE table and update RESULT table like this

pastedImage_5.png

As I have 300+ attributes what will be the efficient way to do this ?

This post has been answered by Stew Ashton on May 20 2019
Jump to Answer
Comments
Post Details
Added on May 17 2019
35 comments
776 views