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!

Flipping the table horizontally

user527060Feb 20 2009 — edited Feb 20 2009
I am using 10g Release 10.2.0.1.0

I've the following table and wanted to flip table horizontally in such a way that for each key if there is a s_key and type_cd, then it checks another table if that column exist in it or not?
if the column doesnt exist, then it should create a new column in another table.

Table_A
KEY    S_KEY    TYPE_CD 

191    100   A    
191    50    A    
191    70    A    
191    70    C 
100    10    A
100    20    C
flip it is this manner
skey_100  skey_50  skey_70  type_A  type_C
skey_10    skey_20 type_A  type_C
Lets only look at KEY 191.
now i want to check another table TABLE_B if ( columns skey_100 , skey_50, skey_70, type_A, type_C are present) if not then add to that table with datatype varchar2(10)

here is my effort
FOR c IN (SELECT DISTINCT S_KEY, TYPE_CD 
             FROM TABLE_A
             WHERE KEY = 191
             )
             
              LOOP
                 BEGIN
                 
                        SELECT distinct skey, type_cd
                              INTO c_column_name
                        FROM all_tab_columns a
                        WHERE owner = 'STG'
                        AND table_name = 'TABLE_A'
                        AND column_name = 'SKEY_'||c.S_KEY ; 
     
                       EXCEPTION WHEN NO_DATA_FOUND THEN
                       EXECUTE IMMEDIATE 'ALTER TABLE TABLE_A ADD ' 
                                        || 'SKEY_'||c.S_KEY
                                        || ' VARCHAR(10)';
                 END;
              END LOOP;
I dont know how to do both the colums together..


Thanks
This post has been answered by 667579 on Feb 20 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 20 2009
Added on Feb 20 2009
9 comments
542 views