在 pgAdmin 4 中创建触发器函数
Creating Trigger function in pgAdmin 4
我正在尝试使用 pgAdmin 4 在 PostgreSQL 数据库中创建一个触发器函数。该函数应该执行 pg_notify 和 return 在 JSON 中新插入的数据。但是我遇到了错误,无法弄清楚哪里出错了。
代码:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$$;$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
错误:
enter code here
ERROR: syntax error at or near "CREATE"
LINE 5: AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
^
解决方法是:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
您将函数定义再次嵌套到创建函数语句中:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE plpgsql
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
我正在尝试使用 pgAdmin 4 在 PostgreSQL 数据库中创建一个触发器函数。该函数应该执行 pg_notify 和 return 在 JSON 中新插入的数据。但是我遇到了错误,无法弄清楚哪里出错了。
代码:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$$;$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
错误:
enter code here
ERROR: syntax error at or near "CREATE"
LINE 5: AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
^
解决方法是:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
您将函数定义再次嵌套到创建函数语句中:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE plpgsql
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;