且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Oracle PLSQL 中透视表?

更新时间:2023-01-16 17:14:20

为了好玩:我用 PL/SQL 循环和动态 SQL 填充旧表中的新表.这不是我们经常做的事情,但为什么不为一次性任务做呢?

For the fun of it: I am filling the new table from the old one with a PL/SQL loop and dynamic SQL. This is nothing we would regularly do, but why not do it for a one-time task?

begin
  for col in
  (
    select column_name
    from user_tab_cols
    where table_name = 'OLDTABLE' and regexp_like(column_name, '^[[:digit:]]{4}$')
  ) loop
    execute immediate
      'insert into newtable (key, year, value)
       select key, ' || col.column_name || ', "' || col.column_name || '" from oldtable';
  end loop;
  commit;
end;