PL / pgSQL FOR循环中的语法错误

时间:2014-04-02 16:46:34

标签: sql postgresql loops plpgsql

我正在尝试使用FOR循环:

create or replace function update_revisions() returns trigger as 
$$ 
begin 
    declare col_name declare col_name information_schema.columns%ROWTYPE; 

    for col_name in
       select column_name from information_schema.columns
       where table_name='t'
    loop 
        insert into debug_table values (col_name); 
    end loop;
end;
$$
language plpgsql; 

但它总是说:

  'for'或其附近的

语法错误

有人可以给我一个暗示它有什么问题吗?

1 个答案:

答案 0 :(得分:1)

即时问题

语法无效。解开:

create or replace function update_revisions()
  returns trigger as 
$$ 
declare
   col_name information_schema.columns%ROWTYPE; 
begin 
   for col_name in
      select column_name from information_schema.columns
      where table_name='t'
   loop 
      insert into debug_table values (col_name); 
    end loop;
end;
$$  language plpgsql;

更多问题

INSERT INTO debug_table (target_column)  -- with column definition list!
SELECT column_name
FROM   information_schema.columns
WHERE  table_name = 't'
AND    table_schema = 'public';          -- your schema