CREATE PROCEDURE test()
BEGIN
SELECT 'hello, I am a Procedure' AS hi;
END
Error:
ERROR: syntax error (example position: "SELECT")
LINE 3: SELECT 'hello, I am a Procedure' AS hi;
^
ERROR: syntax error (example position: "SELECT")
SQL state: 42601
Character: 32
I’m trying to create a procedure but nothing works and I don’t know what to do, I’ve surfed the Internet. I use sql Scrapbook (eclipse (java)), I tried to run the query through pgAdmin 4, it didn’t work either.
2
Answers
For Postgres, the
CREATE PROCEDURE
documentation says you can either use theAS $$ ... $$
or theBEGIN ATOMIC ... END
syntax:For SQL Server, a procedure without parameters is declared without parentheses, and don’t forget
AS
:You attempt to return a string. A
PROCEDURE
can (as of Postgres 16) only return a single row by way of usingINOUT
orOUT
parameters. So your attempt translates to:Or the "SQL standard" variant in Postgres 14 or later:
Or the short form for this simple case:
And you have to
CALL
a procedure to execute:We can omit the
INOUT
parameter in the call because we declaredNULL
as default value (as we should).See:
I suspect you just fell for the widespread misnomer "stored procedure" and really want a
FUNCTION
.