Question: PostgreSQL Function Syntax Error in DECLARE clause

Question

PostgreSQL Function Syntax Error in DECLARE clause

Answers 2
Added at 2016-12-01 10:12
Tags
Question

I am unable to create this function as I get syntax error near INTEGER in line 6. Am I missing something here?

CREATE OR REPLACE FUNCTION public.update_application_status_by_token(applicationToken integer, userId integer, newStatus data.applicationstatus)
  RETURNS BOOLEAN
  LANGUAGE sql
AS $function$
    DECLARE
      applicationId INTEGER;
    BEGIN
      SELECT id INTO applicationId FROM data.listings_applications WHERE token = applicationToken;
      INSERT INTO public.listings_applications_status(application_id, user_id, status)  VALUES (applicationId, userId, newStatus);
      UPDATE public.listings_applications SET status = newStatus WHERE token=applicationToken;
      RETURN TRUE;
      EXCEPTION WHEN OTHERS THEN
        RETURN FALSE;
    END;
$function$
Answers to

PostgreSQL Function Syntax Error in DECLARE clause

nr: #1 dodano: 2016-12-01 10:12

This is a PL/pgSQL function, so you must declare it as such.

Use

LANGUAGE plpgsql

rather than

LANGUAGE sql
nr: #2 dodano: 2016-12-01 11:12

change

LANGUAGE sql

to

LANGUAGE plpgsql

to make it work. SQL DECLARE is for cursors. In your function it is plpgslq command

Source Show
◀ Wstecz