Thread: simple test code

simple test code

From
java4dev
Date:
Hello,

new to Postgresql and have a problem.
I have been using Oracle and MySQL up to now and I was thinking to give
PostgreSQL a try.

I have opened a SQL console from the pgAdmin tool and tried to run the
following

DECLARE
 time1 TIMESTAMP (3);
 time2 TIMESTAMP(3);
 diff INTERVAL;
BEGIN
 time1 := now();
 diff := INTERVAL '30' second;
 time2 := time1 + diff;
 SELECT time1, time2;
END;

but I get an error that I cannot interpret

ERROR:  syntax error at or near "TIMESTAMP"
LINE 3:  time1 TIMESTAMP (3);
               ^

********** Error **********

ERROR: syntax error at or near "TIMESTAMP"
SQL state: 42601
Character: 17

I also tryed to add a label in the beginning

<<test>>
DECLARE
 time1 TIMESTAMP (3);
 time2 TIMESTAMP(3);
 diff INTERVAL;
BEGIN
 time1 := now();
 diff := INTERVAL '30' second;
 time2 := time1 + diff;
 SELECT time1, time2;
END;

but then I get

ERROR:  syntax error at or near "<<"
LINE 1: <<test>>
        ^


********** Error **********

ERROR: syntax error at or near "<<"
SQL state: 42601
Character: 1



I guess I am doing something wrong but what?
--
View this message in context: http://postgresql.1045698.n5.nabble.com/simple-test-code-tp3395857p3395857.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.

Re: simple test code

From
Tom Lane
Date:
java4dev <java4dev@gmail.com> writes:
> I have opened a SQL console from the pgAdmin tool and tried to run the
> following

> DECLARE
>  time1 TIMESTAMP (3);
>  time2 TIMESTAMP(3);
>  diff INTERVAL;
> BEGIN
>  time1 := now();
>  diff := INTERVAL '30' second;
>  time2 := time1 + diff;
>  SELECT time1, time2;
> END;

> I guess I am doing something wrong but what?

It looks like you're trying to run pl/pgsql code directly as SQL.
Postgres draws a distinction between those things, much more sharply
than Oracle does.  You need to create a function to run pl/pgsql
code in.  See the examples in the pl/pgsql part of the manual.

(If you're using PG 9.0, you only need a DO block, so the notational
overhead for a throwaway piece of code is a lot less ... but it's
still a distinct environment.)

            regards, tom lane

Re: simple test code

From
Vibhor Kumar
Date:
>
> DECLARE
> time1 TIMESTAMP (3);
> time2 TIMESTAMP(3);
> diff INTERVAL;
> BEGIN
> time1 := now();
> diff := INTERVAL '30' second;
> time2 := time1 + diff;
> SELECT time1, time2;
> END;
>
> but I get an error that I cannot interpret
>
> ERROR:  syntax error at or near "TIMESTAMP"
> LINE 3:  time1 TIMESTAMP (3);
>               ^
>
> ********** Error **********
>
> ERROR: syntax error at or near "TIMESTAMP"
> SQL state: 42601
> Character: 17
>
> I also tryed to add a label in the beginning
>
> <<test>>
> DECLARE
> time1 TIMESTAMP (3);
> time2 TIMESTAMP(3);
> diff INTERVAL;
> BEGIN
> time1 := now();
> diff := INTERVAL '30' second;
> time2 := time1 + diff;
> SELECT time1, time2;
> END;
>
> but then I get
>
> ERROR:  syntax error at or near "<<"
> LINE 1: <<test>>
>        ^
>
>
> ********** Error **********
>
> ERROR: syntax error at or near "<<"
> SQL state: 42601
> Character: 1
>

This kind of anonymous blocks are not supported in PostgreSQL.
If you are using PG 9.0 then you can try following:
do $$
DECLARE
time1 TIMESTAMP (3);
time2 TIMESTAMP(3);
diff INTERVAL;
BEGIN
time1 := now();
diff := INTERVAL '30' second;
time2 := time1 + diff;
RAISE NOTICE '% %', time1, time2;
END;$$ language plpgsql;


Thanks & Regards,
Vibhor Kumar

Re: simple test code

From
java4dev
Date:
ok thank you.

things certainly work different here.
--
View this message in context: http://postgresql.1045698.n5.nabble.com/simple-test-code-tp3395857p3395921.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.