我正在尝试使用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'或其附近的语法错误
有人可以给我一个暗示它有什么问题吗?
答案 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;
表名在Postgres数据库中不是唯一的。最近的回答更多:
Behaviour of NOT LIKE with NULL values
整个方法效率低下。请改用一个INSERT
语句:
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