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!

Pass Column Name as an Argument and use that in Select query

User_5T7BPMay 17 2019 — edited May 20 2019

Hi All

I need to pass a column Name as an argument and  use that in the select query of an update statement,How can i do that ?My update is using the merge statement like below

MERGE INTO RESULT RES

USING (select FIRST_NAME,EMPLOYEE_ID from EMPLOYEE) EMP

ON (RES.EID  = EMP.EMPLOYEE_ID)

WHEN MATCHED THEN

    UPDATE SET RES.DATA_VAL = EMP.FIRST_NAME

                         WHERE RES.EID = EMP.EMPLOYEE_ID

          and RES.key=1;

the key and the attribute I have stored in another table  like

below

KEY  ATTR_NAME

1     FISRT_NAME

2     LAST_NAME

3   CITY

When I pass the key as an argument to the above script  the select statement should change based on the corresponding attribute name in second table, ie if I pass key as 3 to the above script it should change to

MERGE INTO RESULT RES

USING (select CITY,EMPLOYEE_ID from EMPLOYEE) EMP

ON (RES.EID  = EMP.EMPLOYEE_ID)

WHEN MATCHED THEN

    UPDATE SET RES.DATA_VAL = EMP.CITY

                         WHERE RES.EID = EMP.EMPLOYEE_ID

          and RES.key=3;  (FIRST_NAME changed to CITY)

how can I do this ? I tried to use dynamic SQL but I am not able to use that in the select statement of an update statement please help me

This post has been answered by Stew Ashton on May 17 2019
Jump to Answer
Comments
Post Details
Added on May 17 2019
38 comments
5,622 views