Re: Asignacion del resultado de un select para usarlo como argumenti en otro select

From: Juanky Moral <juanky(dot)moral(at)gmail(dot)com>
To: Luis Gonzalez <luis(dot)gonzalez(at)hct(dot)ac(dot)ae>
Cc: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Asignacion del resultado de un select para usarlo como argumenti en otro select
Date: 2005-07-21 10:16:27
Message-ID: 463a53a405072103162f2e79cc@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Si lo que quieres hacer es obtener valores de un SELECT y volcarlos en
variables, entonces debes usar SELECT INTO nombre_variable
nombre_atributo FROM ...
Si por el contrario, no te interesa obtener ningún resultado, entonces
debes usar PERFORM.

El 21/07/05, Luis Gonzalez<luis(dot)gonzalez(at)hct(dot)ac(dot)ae> escribió:
> Estimados
>
> Estoy migrando una BD que tenia en M$ a postgres, y he quedado atrapado en
> un procedure que converti a funcion, pero al ejecutar se cae con el error
> que adjunto:
>
> El codigo es:
>
> CREATE OR REPLACE FUNCTION "public"."sp_resumen_netflow" (numeric) RETURNS
> boolean AS
> declare id_tnet alias for $1;
> declare dia_hora char(16);
> declare ipfw char(15);
> BEGIN
> select substring(nfile,char_length(nfile)-14,char_length(nfile)-14) as
> dia_hora, substring(nfile, 1,char_length(nfile)-16) as ipfw
> from tnetflow where (id = $1);
> if not exists (select * from fecha_netflow where fecha = dia_hora
> and ipfw = ipfw)
> then
> insert into fecha_netflow(fecha, ipfw) values (dia_hora, ipfw);
> end if;
> ...
> return 1;
> END;
> $body$
> LANGUAGE 'plpgsql' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;
>
>
> El primer select esta garantizado que se refiere solo a una fila de la tabla
> ya que id es la PK, por lo tanto la idea es rescatar el valor de día_hora e
> ipfw para usarlos como argumentos en otros select.
>
> El error es
> ERROR: syntax error at or near "$1" at character 74
> QUERY: SELECT substring(nfile,char_length(nfile)-14,char_length(nfile)-14)
> as $1 , substring(nfile, 1,char_length(nfile)-16) as $2 from tnetflow
> where (id = $3 )
> CONTEXT: PL/pgSQL function "sp_re
>
>
> Tratando de hacer algo cambie el primer select a una variable de texto, algo
> asi como
>
> sqltext= "select
> substring(nfile,char_length(nfile)-14,char_length(nfile)-14) as dia_hora,
> substring(nfile, 1,char_length(nfile)-16) as ipfw from tnetflow where (id =
> " !! id_tnet !! ");";
> execute sqltext;
>
> En este caso el error
>
> ERROR: column "select
> substring(nfile,char_length(nfile)-14,char_length(nfile)" does not exist
> CONTEXT: SQL statement "SELECT "select
> substring(nfile,char_length(nfile)-14,char_length(nfile)-14) as dia_hora,
> substring(nfile, 1,char_length(nfile)-16) as
>
>
> Please, me podrian ayudar a clarificar donde esta el error, o mejor aun como
> traspasar los resultados del primer select a variables, para su uso
> posterior.
>
> Gracias
>
> _____________
> Luis A. González
>
>
>
> ---------------------------(fin del mensaje)---------------------------
> TIP 3: si publicas/lees desde Usenet, por favor envía "subscribe-nomail"
> a majordomo(at)postgresql(dot)org para que tus mensajes puedan llegar
> a los suscriptores de la lista
>

--
Juanky Moral
"Tendré que moverme más rápido: el horizonte brilla eléctrico."
(Horizonte Eléctrico - www.losdeltonos.com )

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Juanky Moral 2005-07-21 10:57:43 Re: problema con una funcion
Previous Message David Prieto 2005-07-21 06:18:32 RE: A Access o Excel