I am very new to postgres. I got this error when try to run the following script:
CREATE OR REPLACE FUNCTION xyz(text) RETURNS INTEGER AS
'DECLARE result int;
BEGIN
SELECT count(*) into result from tbldealercommissions
WHERE
txtdealercode = $1;
if result < 1 then returns 1;
else returns 2 ;
end if;
END;
'
LANGUAGE sql VOLATILE;
The error is
ERROR: syntax error at or near "int"
LINE 3: 'DECLARE result int;
not sure what cause this error. Any help is appreciated.
This is unsuitable:
use this instead:
The syntax you are trying to use is not pure SQL language but the procedural PL/pgSQL language. In PostgreSQL you can install different languages and PL/pgSQL is only primus inter pares in that regard. This also means that you might get the error message, that this language is not installed. In that case use
which actives it. Depending on the version of PostgreSQL you might need superuser rights to do this step.
Have fun.