MoYi123
2020-05-26 19:34:08 +08:00
create table test1 (a int);
create table test2 (b int);
insert into test1 values (1);
insert into test2 values (2);
create or replace FUNCTION f(tb text, val1 integer)
returns TABLE
(
val int
)
as
$body$
BEGIN
IF EXISTS(SELECT a.attname
from pg_class c,
pg_attribute a,
pg_type t
where c.relname = tb
and a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
and a.attname = 'a') THEN
return query execute 'select a from ' || tb || ' where a = ' || val1;
ELSIF EXISTS(SELECT a.attname
from pg_class c,
pg_attribute a,
pg_type t
where c.relname = tb
and a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
and a.attname = 'b') THEN
return query execute 'select b from ' || tb || ' where b = ' || val1;
END IF;
END;
$body$
language plpgsql;
lz 可以改得简洁一点