I have this function in postgressql, the p_flg_init set the if branch :
CREATE OR REPLACE FUNCTION dwh.f_dwh_post_dm_updt_vend_cust_serv(p_flg_init varchar DEFAULT 'N')
RETURNS text
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
proc_step varchar := NULL;
nTemId numeric := NULL;
qta_nobilia numeric := NULL;
vcErrore varchar(4000);
vcStato varchar(10); -- (N)ormale / (W)arning / (E)rrore / (NA) Non Eseguito
idProcedura numeric := 0;
codProcedura varchar(50) := 'POST_DM_UPDT_VEND_CUST_SERV';
v_state TEXT;
v_msg TEXT;
v_detail TEXT;
v_hint TEXT;
v_context TEXT;
ANNOMESE RECORD;
begin
IF(p_flg_init='Y')
then
begin
proc_step := 'LOOP ANNOMESE';
BEGIN
FOR ANNOMESE
IN (
SELECT
mf_id FROM d_tempo WHERE mf_id
BETWEEN To_Char(current_date-1+interval '-24 month','YYYYMM')::integer and To_Char(current_date-1+interval '-1 month','YYYYMM')::integer
-- intervallo rolling 12 mese chiuso
)
LOOP
statement in loop ;
END LOOP;
end;
else
begin
statement that not use lopp ;
end;
end if;
END;
$function$
;
when I compile the code the error : SQL Error [42601]: ERROR: syntax error at or near "else"
Position: 5669 appears.
If I write the function without using else it works fine
2
Answers
I've removed begin and end and now I have different error in exception : ERROR: syntax error at or near "exception" LINE 281: exception
The main issue is missing
END
keyword beforeELSE
. This is not Pascal language, you don’t need to use explicit blocks everywhere. Try to read documentation first, please.Your code (wrong):
Correct version: