SYNTAX ERROR ON FOR... LOOP

Поиск
Список
Период
Сортировка
Искать

SYNTAX ERROR ON FOR... LOOP

От:
Rodrigo Carvalhaes <grupos@carvalhaes.net>
Дата:
Hi Guys,

I am having a "simple syntax problem" but very strange...

I am trying to make an IF / ELSE / END IF inside of a FOR ... LOOP but I 
am getting syntax error on this contol structure...
If I comment the IF / ELSE / ENDIF the fuction works ...
I am using PostgreSQL win 8.0
Any tip ? :-D

The function is:

CREATE OR REPLACE FUNCTION reorder() RETURNS int4 AS
$BODY$

DECLARE   new_code     INTEGER;   recs          RECORD;   validation   varchar;   vstrname      varchar;   vstrcgc_cic   varchar;   vstrquery     varchar;   vstrupdate    varchar;

BEGIN

new_code := 1;

FOR recs IN (SELECT * FROM table1 ORDER BY is_customer, name)
LOOP

-- Validation
vstrname := replace(recs.name,$$'$$,$$\'$$);

vstrupdate := $$UPDATE table1 SET code = $$ || new_code ||
$$ WHERE old_code = $$ || recs.old_code || $$ AND is_customer $$;

IF records.is_customer IS FALSE THEN
vstrupdate := vstrupdate || $$ IS true $$;
ELSE
vstrupdate := vstrupdate || $$ IS false $$;
ENDIF;

EXECUTE vstrupdate;

new_code := new_code + 1 ;END LOOP;RETURN 1;
END;

$BODY$ LANGUAGE 'plpgsql' VOLATILE;

My table1 is:
   CREATE TABLE table1
( code int4, name varchar(50), old_code int4, is_customer bool DEFAULT false
)
WITHOUT OIDS;

INSERT INTO table1 (code, name, old_code, is_customer) VALUES (9, 
'John', 23, true);
INSERT INTO table1 (code, name, old_code, is_customer) VALUES (19, 
'Rodrigo', 334, true);
INSERT INTO table1 (code, name, old_code, is_customer) VALUES (82, 
'Fulano', 3484, true);
INSERT INTO table1 (code, name, old_code, is_customer) VALUES (1, 
'Beltrano', 3454, false);
INSERT INTO table1 (code, name, old_code, is_customer) VALUES (4, 
'Madicon', 23, false);

Cheers,

Rodrigo Carvalhaes


-- 
Esta mensagem foi verificada pelo sistema de antivírus eacredita-se estar livre de perigo.


Re: SYNTAX ERROR ON FOR... LOOP

От:
Michael Fuhr <mike@fuhr.org>
Дата:
On Wed, Apr 27, 2005 at 02:39:53PM -0300, Rodrigo Carvalhaes wrote:
> 
> I am trying to make an IF / ELSE / END IF inside of a FOR ... LOOP but I 
> am getting syntax error on this contol structure...

[snip]

> IF records.is_customer IS FALSE THEN
> vstrupdate := vstrupdate || $$ IS true $$;
> ELSE
> vstrupdate := vstrupdate || $$ IS false $$;
> ENDIF;

The above should be "END IF" (with a space).

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Re: SYNTAX ERROR ON FOR... LOOP

От:
George Weaver <gweaver@shaw.ca>
Дата:
Hi Rodrigo,


----- Original Message ----- 
From: "Rodrigo Carvalhaes" 
To: 
Sent: Wednesday, April 27, 2005 12:39 PM
Subject: [SQL] SYNTAX ERROR ON FOR... LOOP


> Hi Guys,
>
> I am having a "simple syntax problem" but very strange...
>
> I am trying to make an IF / ELSE / END IF inside of a FOR ... LOOP but I 
> am getting syntax error on this contol structure...
> If I comment the IF / ELSE / ENDIF the fuction works ...
>
snip

> vstrupdate := $$UPDATE table1 SET code = $$ || new_code ||
> $$ WHERE old_code = $$ || recs.old_code || $$ AND is_customer $$;
>
> IF records.is_customer IS FALSE THEN
> vstrupdate := vstrupdate || $$ IS true ;$$;                                                         ^^^^
I believe you need an ";" after true and false to complete the string as an 
SQL statement

> ELSE
> vstrupdate := vstrupdate || $$ IS false ;$$;                                                       ^^^^

> ENDIF;
>
> EXECUTE vstrupdate;



Re: SYNTAX ERROR ON FOR... LOOP

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Rodrigo Carvalhaes  writes:
> I am trying to make an IF / ELSE / END IF inside of a FOR ... LOOP but I 
> am getting syntax error on this contol structure...
> If I comment the IF / ELSE / ENDIF the fuction works ...

plpgsql wants "END IF" not "ENDIF".
		regards, tom lane

Re: SYNTAX ERROR ON FOR... LOOP

От:
Rodrigo Carvalhaes <grupos@carvalhaes.net>
Дата:
 Guys, sometimes I make stupid things... 

I t was the END IF as all of you said...

Thanks for your exists.

Cheers,

Rodrigo

Tom Lane wrote:
Rodrigo Carvalhaes <grupos@carvalhaes.net> writes: 
I am trying to make an IF / ELSE / END IF inside of a FOR ... LOOP but I 
am getting syntax error on this contol structure...
If I comment the IF / ELSE / ENDIF the fuction works ...   
plpgsql wants "END IF" not "ENDIF".
		regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?
              http://www.postgresql.org/docs/faq
 

-- 


Abraço,

Rodrigo Carvalhaes
DBA PostgreSQL
Moderador grupo siga-br

--
Esta mensagem foi verificada pelo sistema de antivírus e
acredita-se estar livre de perigo.
FAQ