Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/pgSQL syntax error

I have a very simple PL/pgSQL script:

     declare x varchar(100);

When I run it I get a message:

    [WARNING  ] declare x varchar(100)
        ERROR:  syntax error at or near "varchar"
        LINE 1: declare x varchar(100)
                          ^

I really don't understand what is wrong with this.

like image 307
user1544745 Avatar asked Feb 19 '13 17:02

user1544745


People also ask

Can we run PL SQL in PostgreSQL?

PostgreSQL is a procedural programming language. PL/pgSQL was designed to create user-defined functions, stored procedures, and triggers, inherit all user-defined functions, and types, and much more. Getting started with PostgreSQL : First We'll learn how to create a single table using basic PLSQL commands.

What does PL pgSQL mean?

PL/pgSQL (Procedural Language/PostgreSQL) is a procedural programming language supported by the PostgreSQL ORDBMS.

Is used to declare a PL pgSQL?

Another way to declare a PL/pgSQL function is with RETURNS TABLE , for example: CREATE FUNCTION extended_sales(p_itemno int) RETURNS TABLE(quantity int, total numeric) AS $$ BEGIN RETURN QUERY SELECT s. quantity, s.


1 Answers

you can use procedural statements only inside function body in PostgreSQL.

CREATE OR REPLACE FUNCTION foo()
RETURNS int AS 
$$ -- here start procedural part
   DECLARE x int;
   BEGIN
     x := 10;
     RETURN x;
   END;
$$ -- here finish procedural part
LANGUAGE plpgsql; -- language specification 

or in temporary function (anonymous block)

DO $$
DECLARE x int;
BEGIN
  x := 10;
  RAISE NOTICE '>>>%<<<', x;
END;
$$;

isn't possible to use procedural statements as SQL statements like T-SQL.

like image 162
Pavel Stehule Avatar answered Sep 23 '22 06:09

Pavel Stehule