Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 987479FB306; Mon, 18 Jun 2007 11:34:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 53122-08; Mon, 18 Jun 2007 11:34:26 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sr01-01.mta.terra.com.br (sr01-01.mta.terra.com.br [200.154.152.20]) by postgresql.org (Postfix) with ESMTP id F32FA9FBA7B; Mon, 18 Jun 2007 11:34:32 -0300 (ADT) Received: from taragoro.hst.terra.com.br (taragoro.hst.terra.com.br [200.176.10.9]) by bulimba.hst.terra.com.br (Postfix) with ESMTP id B1CF335A012D; Mon, 18 Jun 2007 11:34:30 -0300 (BRT) X-Terra-Karma: -2% X-Terra-Hash: d4fdd812a759efe2a7ce282a134e0624 Received-SPF: pass (taragoro.hst.terra.com.br: domain of terra.com.br designates 200.176.10.9 as permitted sender) client-ip=200.176.10.9; envelope-from=ranieri.oliveira@terra.com.br; helo=[172.16.2.72]; Received: from [172.16.2.72] (200-162-219-207.corp.ajato.com.br [200.162.219.207]) (authenticated user ranieri.oliveira) by taragoro.hst.terra.com.br (Postfix) with ESMTP id D51B150064; Mon, 18 Jun 2007 11:34:29 -0300 (BRT) Message-ID: <467697ED.8030506@terra.com.br> Date: Mon, 18 Jun 2007 11:34:21 -0300 From: Ranieri Mazili User-Agent: Thunderbird 2.0.0.4 (Windows/20070604) MIME-Version: 1.0 To: pgsql-general@postgresql.org, pgsql-sql@postgresql.org Subject: Setting Variable - (Correct) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.756 tagged_above=0 required=5 tests=AWL=-0.153, BAYES_50=0.001, DNS_FROM_RFC_ABUSE=0.2, DNS_FROM_RFC_POST=1.708, UPPERCASE_25_50=0 X-Spam-Level: * X-Archive-Number: 200706/1045 X-Sequence-Number: 115868 Hello, I'm trying do the following function: CREATE OR REPLACE FUNCTION lost_hours_temp(date) RETURNS text AS $BODY$ DECLARE START_DATE date; END_DATE date; QUERY text; BEGIN START_DATE := $1; END_DATE := START_DATE - interval '3 year'; WHILE EXTRACT(YEAR FROM START_DATE) = EXTRACT(YEAR FROM END_DATE)+3 LOOP QUERY := 'SELECT CAST(EXTRACT(YEAR FROM A.production_date) || '/' || EXTRACT(MONTH FROM A.production_date) AS TEXT) as date, SUM(production_hours) AS production_hours, B.id_production_area FROM production A, product B WHERE EXTRACT(MONTH FROM production_date) = EXTRACT(MONTH FROM ' || START_DATE || ') AND EXTRACT(YEAR FROM A.production_date) = EXTRACT(YEAR FROM ' || START_DATE || ') AND lost_hours = ' || 'S' ||' AND A.id_product = B.id_product GROUP BY id_production_area, date'; START_DATE := START_DATE - interval '1 month'; END LOOP; RETURN QUERY; END; $BODY$ LANGUAGE 'plpgsql'; My problem is into WHILE, I'm trying to concatenate variables with the string, but I guess that it's generating an error. What's the correct form to concatenate strings with query in my case? Thanks