x
1
declare
2
internalid integer;
3
begin
4
--DESCRICAO NULA
5
if descricao is null then
6
RAISE EXCEPTION 'descricao = %', descricao
7
USING HINT = 'Field descricao must be not null';
8
end if;
9
10
--SGLTIPOLOCAL NULA
11
if sgltipolocal is null then
12
RAISE EXCEPTION 'sgltipolocal = %', sgltipolocal
13
USING HINT = 'Field sgltipolocal must be not null';
14
end if;
15
16
--IDNATIVO NULO OU FORA DO PADRAO
17
if idnativo is null or idnativo not in (0,1,-99999) then
18
RAISE EXCEPTION 'idnativo = %', idnativo
19
USING HINT = 'Field must not be null or different from 1 or 0.';
20
end if;
21
22
--CODIGOERP NULO
23
if externalid is null or externalid = '' then
24
RAISE EXCEPTION 'externalid = %', externalid
25
USING HINT = 'Field externalid must be not null';
26
end if;
27
28
--VALIDAÇÃO DE CODIGOERP JÁ EXISTENTE
29
IF (select distinct 1 FROM TIPOLOCAL WHERE CODIGOERP = PostTipoLocal.externalid) = 1
30
THEN RAISE EXCEPTION 'externalid = %', PostTipoLocal.externalid
31
USING HINT = 'The record (externalid = '|| PostTipoLocal.externalid ||') already exists, it was not possible to insert it.';
32
ELSE insert into tipolocal (idtipolocal, idnativo, codigoerp, sgltipolocal, descricao, idndisponivelcadastro) values (nextval('seqpktipolocal'),idnativo, externalid, sgltipolocal , descricao, 1 ) returning idtipolocal into internalid;
33
end if;
34
35
return query select internalid;
36
END;