我有一个程序:
create or replace procedure pro_update_last_read(in input_sensor_id integer, in read_time timestamp)
as
$$
begin
update sensor
set last_read = read_time
where sensor_id = input_sensor_id;
end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
以及一个调用它的触发器:
create trigger tri_last_read
after insert or update of report_time
on report
execute procedure pro_update_last_read(sensor_id, report_time);
Run Code Online (Sandbox Code Playgroud)
但是,在创建触发器时,我收到错误消息:
Run Code Online (Sandbox Code Playgroud)[42883] ERROR: function pro_update_last_read() does not exist
为什么会发生这个错误?
我正在尝试返回重叠多边形的数量。问题是它抱怨我的“$1”标志:
Run Code Online (Sandbox Code Playgroud)Error is "SQL state: 42601", syntax error at "$1".
为什么是这样?我在这里完全是初学者,除了用符号引用参数之外,我无法在网上找到解决方案$。
CREATE OR REPLACE FUNCTION any_overlap (x text)
RETURNS integer AS $$
DECLARE amount INTEGER;
BEGIN
SELECT COUNT(*) INTO amount FROM $1 a
INNER JOIN $1 b ON
(a.polygon && b.polygon AND ST_Relate(a.polygon, b.polygon, '2********'))
WHERE a.ctid != b.ctid;
RETURN amount AS id;
END; $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud) 我想更改架构中所有对象的所有者。为此,我使用以下函数:
CREATE OR REPLACE FUNCTION chown(in_schema VARCHAR, new_owner VARCHAR)
RETURNS void AS
$$
DECLARE
object_types VARCHAR[];
object_classes VARCHAR[];
object_type record;
r record;
BEGIN
object_types = '{type,table,sequence,index,table,view}';
object_classes = '{c,t,S,i,r,v}';
FOR object_type IN
SELECT unnest(object_types) type_name,
unnest(object_classes) code
loop
FOR r IN
EXECUTE '
select n.nspname, c.relname
from pg_class c, pg_namespace n
where n.oid = c.relnamespace
and nspname = ''' || in_schema || '''
and relkind = ''' || object_type.code || ''''
loop
raise notice 'Changing ownership of % %.% to …Run Code Online (Sandbox Code Playgroud) 我想使用COPYPostgres 中的函数将数据发送到 .csv 文件。但我使用匿名块,所以我的表名应该是变量的值。
COPY (SELECT cname.portal from user) To '/tmp/out.csv' With CSV;
Run Code Online (Sandbox Code Playgroud)
cname我的匿名块内的变量在哪里。我尝试使用EXECUTE格式,但它也不起作用。我试过类似的东西:
EXECUTE format (' COPY (select * from %s.portal,cname ) To '/tmp/out1.csv' With CSV');
Run Code Online (Sandbox Code Playgroud) plpgsql ×4
postgresql ×4
dynamic-sql ×2
copy ×1
functions ×1
postgis ×1
role ×1
schema ×1
syntax ×1
trigger ×1