9

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.

4
  • Show the whole create function script Commented Feb 19, 2013 at 17:04
  • This is the whole script. Commented Feb 19, 2013 at 17:05
  • 2
    You must wrap it in a create function command. Commented Feb 19, 2013 at 17:12
  • 1
    That is not a complete (and syntactically correct) PL/pgSQL "script" Commented Feb 19, 2013 at 23:04

3 Answers 3

26

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Use exemple

DO $$
Declare
  test varchar;
begin
   test := 'teste';
   if (char_length(test) > 0) then
      RAISE NOTICE '>>>%<<<', test;
   end if;
end;
$$;

Comments

0

I have resolved this by the following piece of code:

do $$
declare TypeId int;
declare MasterId int;
begin
   TypeId := null;
   MasterId := null;
end;
$$;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.