I have run this query to get the result as below, but, even if my query is running fine, that I do not get the expected result.

I'm looking for only column compare to my same target table as a table source.

My query:


Select case when column_name_s is null and column_name_t is not null

then "alter table GRADE_CONVERSION drop | column_name_t | ';'

When column_name_s is not null and column_name_t is null

then "alter table GRADE_CONVERSION add | column_name_s | ' ' || data_type_s | « ; »

else 'alter table modify GRADE_CONVERSION | column_name_t | ' ' || data_type_t | « ; »

alterations of the end

from (select s.column_name column_name_s, t.column_name column_name_t,

s.data_type data_type_s, t.data_type data_type_t

(select column_name, column_id, data_type

of all_tab_cols@database

where owner = 'erhan.

and table_name = "GRADE_CONVERSION."

+ 1

full outer join

(select column_name, column_id, data_type

of all_tab_cols@database

where owner = 'sarigul.

and table_name = "GRADE_CONVERSION."

+ 6

on s.column_name = t.column_name

)




Tables:



Target table: table GRADE_CONVERSION in sarigul@database


LETTER_GRADEVARCHAR2 (2)
GRADE_POINTNUMBER (3.2)
MAX_GRADENUMBER (3)
MIN_GRADENUMBER (3)




Table source: Table GRADE_CONVERSION in erhan@database

LETTER_GRADEVARCHAR2 (2)
GRADE_POINTNUMBER (3.2)
MAX_GRADENUMBER (3)
MIN_GRADENUMBER (3)
CREATED_BYVARCHAR2 (30)
CREATED_DATEDATE
MODIFIED_BYVARCHAR2 (30)
MODIFIED_DATEDATE



want to see output that is similar to this * (please ignore the names of column here it's just a clear example :))


ALTER table Target_table change BOOK_ID Varchar2 (4);

ALTER table Target_table I addSBN_10 Varchar2(13), null;

ALTER table drop TITLE Target_table;

Erhan_toronto wrote:

1.I used src.nullable src_nullable and tgt.nullable tgt_nullable but only show Yes as below: but want to see the result as not null or null

ALTER table TEST_TARGET change the NUMBER of MAX_GRADE (3, 2) Yes

Ok. So it's all about the Yes and the no decoding to Default Null or Not Null, isn't it?

So, to test, change one of the table of sample for NOT NULL columns in the source table, and then run the following query:

with src as
(
  select src.table_name src_table_name, src.column_name src_col_name, src.data_type src_data_type, src.data_length src_data_len, src.data_precision src_data_precision, src.data_scale src_data_scale,
         src.nullable src_nullable
    from user_tab_columns src
   where table_name = 'TEST_SOURCE'
),
tgt as
(
  select tgt.table_name tgt_table_name, tgt.column_name tgt_col_name, tgt.data_type tgt_data_type, tgt.data_length tgt_data_len, tgt.data_precision tgt_data_precision, tgt.data_scale tgt_data_scale,
         tgt.nullable tgt_nullable
    from user_tab_columns tgt
   where table_name = 'TEST_TARGET'
),
col_details as
(
  select src.src_table_name, nvl(tgt.tgt_table_name, first_value(tgt_table_name) over(order by tgt_table_name nulls last)) tgt_table_name,
         src.src_col_name, src.src_data_type, src.src_data_len, src.src_data_precision, src.src_data_scale, src.src_nullable,
         tgt.tgt_col_name, tgt.tgt_data_type, tgt.tgt_data_len, tgt.tgt_data_precision, tgt.tgt_data_scale, tgt.tgt_nullable
    from src
    left outer join tgt
      on (
          src.src_col_name = tgt.tgt_col_name
         )
)
select *
  from (
        select case
                when tgt_data_type != src_data_type or tgt_data_len != src_data_len or tgt_data_precision != src_data_precision or tgt_data_scale != src_data_scale or src_nullable != tgt_nullable
                  then 'alter table ' || tgt_table_name || ' modify ' || tgt_col_name || ' ' || src_data_type || ' (' ||
                  case when src_data_type in ('DATE') then null
                       else
                            case
                              when src_data_type in ('VARCHAR', 'VARCHAR2')
                                then nvl(to_char(src_data_len), ' ') || ') '
                              else  decode(nvl(src_data_precision, -1), -1, null, nvl(to_char(src_data_precision), ' ') || ', ' || nvl(to_char(src_data_scale), ' ') || ')')
                            end
                  end
                  || decode(src_nullable, 'NO', ' NOT NULL', ' DEFAULT NULL')
                when tgt_col_name is null
                  then 'alter table ' || tgt_table_name || ' add ' || src_col_name || ' ' || src_data_type ||
                  case when src_data_type in ('DATE') then null
                       else
                            case
                              when src_data_type in ('VARCHAR', 'VARCHAR2')
                                then nvl(to_char(src_data_len), ' ') || ') '
                              else  decode(nvl(src_data_precision, -1), -1, null, nvl(to_char(src_data_precision), ' ') || ', ' || nvl(to_char(src_data_scale), ' ') || ')')
                            end
                  end
                  || decode(src_nullable, 'NO', ' NOT NULL', ' DEFAULT NULL')
               end alter_statement
          from col_details
        )
where alter_statement is not null;

Erhan_toronto wrote:

2. when I run below under user sarigul and erhan I get the result as OWNER, TABLE_NAME, COLUMN_NAME DATA_TYPE... I have a link between two users. They have access to two tables.

  • Select * from all_tab_columns

where owner = 'erhan' and table_name = "TEST_SOURCE."

  • Select * from all_tab_columns

where owner = 'sarigul' and table_name = "TEST_TARGET."

Alright. This means that you both users are on the same database. Only change, you will have to do in the above query is so change user_tab_columns to all_tab_columns and add the OWNER predicate respectively with the clause.

Tags: Database

Similar Questions

Maybe you are looking for