Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

Backward-compatible SQL query against table with renamed and added column in SQL ?

By M.K. Verma · 4 May 2022

Backward-compatible SQL query against table with renamed and added column in SQL ?

You can check if the colum exists and then run the appropriate `SELECT

DECLARE
  v_column_exists number := 0;  
BEGIN
  Select count(*) into v_column_exists
    from user_tab_cols
    where upper(column_name) = 'b'
      and upper(table_name) = 't1';

  if (v_column_exists = 1) then
      execute immediate 'select a, b as c, null as d from t1';
  ELSE
      execute immediate 'select a, c, d from t2';
  end if;
end;
/