我需要创建一个从查询中获取数据并将其插入另一个表
的函数这是sotred程序
CREATE OR REPLACE FUNCTION public.update_sensor_event_process_t()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
INSERT INTO public.sensor_event_process (id_regra, regra, id_veiculo, id_cliente, velocidade, odometro, data_posicao, ibutton, latitude, longitude, status)
values
(NEW.id_regra, NEW.regra, NEW.id_veiculo, NEW.id_cliente, NEW.velocidade, NEW.odometro, NEW.data_posicao, NEW.ibutton, NEW.latitude, NEW.longitude, NEW.status);
DROP TABLE IF EXISTS tmp;
CREATE temporary TABLE tmp AS SELECT DISTINCT re.id_alerta,
re.nome_alerta,
re.id_tipo_regra,
COALESCE(sep.data_posicao, -1) AS data_posicao,
sep.velocidade,
sep.odometro,
sep.latitude,
sep.longitude,
sep.ibutton,
re.id_motorista_responsavel,
re.placa_veiculo,
re.apelido_veiculo,
re.id_cliente,
re.id_veiculo
FROM public.rule_event re
JOIN public.rule_event re2
ON re.id_alerta = re2.id_alerta
LEFT JOIN public.sensor_event_process sep
ON sep.id_veiculo = re.id_veiculo
AND sep.id_regra = re.id_tipo_regra
AND sep.data_posicao = new.data_posicao
WHERE re.id_veiculo = new.id_veiculo;
DELETE
FROM tmp
WHERE id_alerta IN
(
SELECT id_alerta
FROM tmp
WHERE data_posicao = -1 ) ;
INSERT INTO public.device_event
(
id_device_event,
id_cliente,
id_veiculo,
placa_veiculo,
apelido_veiculo,
id_evento,
id_motorista,
gmt_evento,
nome_motorista,
data_evento,
latitude,
longitude,
ignicao,
velocidade,
ibutton
)
SELECT nextval('device_event_id_device_event_seq' :: regclass),
id_cliente,
id_veiculo,
placa_veiculo,
apelido_veiculo,
id_alerta,
id_motorista_responsavel,
'',
'',
data_posicao,
new.latitude,
new.longitude,
new.ignicao,
new.velocidade,
new.ibutton
FROM tmp;
DROP TABLE IF EXISTS tmp;
select pipeline_kafka.produce_message('topicNotificationProcess', (select '{"eventSummary":' || Row_to_json(q)::text || '}'
FROM (SELECT *
FROM public.device_event
ORDER BY 1 DESC
LIMIT 1) AS q )::bytea );
RETURN new;
END; $函数$
但是数据库会返回错误:
错误:查询没有结果数据的目的地提示:如果您愿意 丢弃SELECT的结果,改为使用PERFORM。背景: SQL中的PL / pgSQL函数update_sensor_event_process_t()第77行 声明STATEMENT:sensor_event_process_transform
答案 0 :(得分:0)
如果您不想将所选数据存储到变量中,则必须使用perform:
PERFORM pipeline_kafka.produce_message('topicNotificationProcess', (select '{"eventSummary":' || Row_to_json(q)::text || '}'
FROM (SELECT *
FROM public.device_event
ORDER BY 1 DESC
LIMIT 1) AS q )::bytea );