2017-02-12 8 views
0

Мне нужно создать функцию, которая принимает данные из запроса и вставляет его в другую таблицусоздать функцию вставки с выберите Postgre

здесь накоп лен процедуру

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; $ функции $

Но databe возвращает ошибку:

ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function update_sensor_event_process_t() line 77 at SQL statement STATEMENT: sensor_event_process_transform

ответ

0

Вы должны использовать выполнить, если вы не хотите, чтобы сохранить выбранные данные в переменную:

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 ); 
+0

мой ответ вам помочь? –

+0

да Роман, большое спасибо, это решило мою проблему ... –