Basic FOR LOOP

Basic question, my loop is like this. Retreiving all the columns from data dictionary and do some mappings...

FOR user_tab_col IN
            (SELECT column_name FROM user_tab_columns
             WHERE table_name = <table_name>
             AND column_name LIKE 'V5%'
            ORDER BY column_name)
LOOP
 --- Mappings ---
END LOOP;

I want to add 1 more condition to check the column exists in another table (new table) or not.

If exist I will do the mappings, If not I will assign it to Null value. Either case I need the column name.

Please can anyone suggest the better way to fix this.
Subhashini ElangoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
I'm not sure I understand what you are after.

Once you have a column name in the loop, just query for that column in another table?

for user_tab_col in ...
loop

     begin
         select count(*) into does_exist from user_tab_columns where column_name=user_tab_col.column_name and table_name <> user_tab_col.table_name;
         exception when no_data_fount then
               --do something with your null
     end;

end loop;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnsoneSenior Oracle DBACommented:
Or,  just add it to the where clause in the loop query.

FOR user_tab_col IN
            (SELECT column_name FROM user_tab_columns
             WHERE table_name = <table_name>
             AND column_name LIKE 'V5%'
             AND EXISTS (select 1 from <other_table> where ....)
            ORDER BY column_name)
LOOP
 --- Mappings ---
END LOOP;
0
awking00Commented:
declare
v_exist number;
begin
for ...
loop
select count(*) into v_exist from user_tab_columns
where column_name = user_tab_col.column_name
and table_name = 'OTHER_TABLE'
if v_exist = 0 then assign NULL
else do_mappings
end if;
end loop;
end;
/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.