Blz. vou alterar e testar.
Obrigado.

Em 16 de julho de 2010 14:05, JotaComm <jota.c...@gmail.com> escreveu:

> Opa,
>
> Em 16 de julho de 2010 13:59, Torgge <tor...@gmail.com> escreveu:
>
>> Ok. Obrigado.
>>
>
> Vamos lá:
>
> Este teste if (nid is not null) then você não precisa fazer:
>
> Defina função como RETURNS NULL ON NULL INPUT, assim quando o valor de
> entrada for nulo não executa a função é retorna null para você.
>
> Coloque um BEGIN e um END; para definir o contexto da exceção, tem isso no
> exemplo da minha função.
>
>
> Alterei a function para:
>>
>> CREATE OR REPLACE FUNCTION "public"."moeda_d" (nid integer) RETURNS
>> boolean AS
>> $body$
>>
>> begin
>>
>>     if (nid is not null) then
>>
>>         savepoint p1;
>>
>>
>>
>>             delete from public.moeda
>>
>>                 where public.moeda.id = nid;
>>
>>
>>
>>        rollback to p1;
>>
>>
>>
>>         update public.moeda set
>>
>>
>>             ativo = 0,
>>
>>             datalt = now()
>>
>>         where id = nid;
>>
>>
>>
>>     end if;
>>
>>
>>
>>     RETURN TRUE;
>>
>>
>>
>> exception
>>
>>     when foreign_key_violation then
>>
>>         RETURN false;
>>
>> end;
>> $body$
>>
>> LANGUAGE 'plpgsql'
>> VOLATILE
>> CALLED ON NULL INPUT
>> SECURITY INVOKER
>> COST 100;
>>
>> mas agora da erro no savepoint: ERRO:  SPI_execute_plan failed executing
>> query "savepoint p1": SPI_ERROR_TRANSACTION
>> CONTEXT:  PL/pgSQL function "moeda_d" line 5 at SQL statement
>>
>> Em 16 de julho de 2010 11:44, JotaComm <jota.c...@gmail.com> escreveu:
>>
>> Olá,
>>>
>>> Em 16 de julho de 2010 11:21, Torgge <tor...@gmail.com> escreveu:
>>>
>>> Mas isso não é exclusão física ? Vou tentar alterar a exception others
>>>> para essa que me passou foreign_key_violation.
>>>>
>>>
>>>
>>> Sim. E para excluir fisicamente. Agora se você quer excluir logicamente
>>> faça um tratamento diferente, pois apenas coloquei uma idéia de como você
>>> poderia trabalhar.
>>>
>>>
>>>
>>>
>>>> Em 16 de julho de 2010 10:38, JotaComm <jota.c...@gmail.com> escreveu:
>>>>
>>>> Olá,
>>>>>
>>>>> Que tal fazer um tratamento assim:
>>>>>
>>>>> Em 16 de julho de 2010 10:22, Torgge <tor...@gmail.com> escreveu:
>>>>>
>>>>> Isso é para exclusão lógica, meu campo ativo controla isso. Porém faço
>>>>>> o delete para verificar se o registro esta linkado a alguma
>>>>>> outra tabela, se o comando delete der exception quer dizer que não
>>>>>> posso exclui-lo então "não posso exclui-lo logicamente tb",
>>>>>> se não der exception no delete, quer dizer que "eu posso exclui-lo
>>>>>> logicamente", o problema é que esta dando exception no
>>>>>> update. Então sempre esta retornando false, quando executo essa
>>>>>> function no debug do (EMS Postgres) ele retorna true,
>>>>>> mas quando executo ele normalmente retorna false.
>>>>>>
>>>>>
>>>>> Que tal fazer um tratamento assim:
>>>>>
>>>>> CREATE OR REPLACE FUNCTION f_delete(INTEGER)
>>>>>
>>>>> RETURNS BOOLEAN AS $$
>>>>>
>>>>> BEGIN
>>>>>
>>>>>     BEGIN
>>>>>
>>>>>     DELETE FROM tabela WHERE codigo=$1;
>>>>>
>>>>>     EXCEPTION WHEN foreign_key_violation THEN
>>>>>
>>>>>         RAISE NOTICE 'Registro % não pode ser excluído.',$1;
>>>>>
>>>>>         RETURN FALSE;
>>>>>
>>>>>     END;
>>>>>
>>>>> RAISE NOTICE 'Registro pode ser excluído: %',$1;
>>>>>
>>>>> RETURN TRUE;
>>>>>
>>>>> END;
>>>>>
>>>>> $$ LANGUAGE PLPGSQL RETURNS NULL ON NULL INPUT;
>>>>>
>>>>>
>>>>>> Em 16 de julho de 2010 09:05, JotaComm <jota.c...@gmail.com>escreveu:
>>>>>>
>>>>>> Olá,
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Em 16 de julho de 2010 08:56, Torgge <tor...@gmail.com> escreveu:
>>>>>>>
>>>>>>> Bom dia.
>>>>>>>> Uma dúvida com a uma funcion:
>>>>>>>> CREATE OR REPLACE FUNCTION padrao.moeda_d
>>>>>>>> (
>>>>>>>>   nid  integer
>>>>>>>> )
>>>>>>>> RETURNS boolean AS
>>>>>>>> $$
>>>>>>>> begin
>>>>>>>>     if (nid is not null) then
>>>>>>>>         savepoint p1;
>>>>>>>>
>>>>>>>>             delete from padrao.moeda
>>>>>>>>                 where padrao.moeda.id = nid;
>>>>>>>>
>>>>>>>>        rollback to p1;
>>>>>>>>
>>>>>>>>         update padrao.moeda set
>>>>>>>>             ativo = 0,
>>>>>>>>             datalt = now()
>>>>>>>>         where id = nid;
>>>>>>>>
>>>>>>>>     end if;
>>>>>>>>
>>>>>>>>     RETURN TRUE;
>>>>>>>>
>>>>>>>> exception
>>>>>>>>     when others then
>>>>>>>>         RETURN false;
>>>>>>>> end;
>>>>>>>> $$
>>>>>>>> LANGUAGE 'plpgsql'
>>>>>>>> VOLATILE
>>>>>>>> CALLED ON NULL INPUT
>>>>>>>> SECURITY INVOKER
>>>>>>>> COST 100;
>>>>>>>>
>>>>>>>> Ela sempre retorna false, quando passa pelo update da exception,
>>>>>>>> alguma sugestão de como fazer corretamente ?
>>>>>>>>
>>>>>>>
>>>>>>> Sempre que cair na excpetion vai retornar false. Não isso que você
>>>>>>> quer?
>>>>>>>
>>>>>>> O que você quer fazer exatamente? Não entendi muito bem sua idéia,
>>>>>>> pois você faz um delete e depois faz o rollback para o seu savepoint
>>>>>>> definido e depois faz um update. Poderia explicar sua idéia, assim fica 
>>>>>>> mais
>>>>>>> fácil ajudarmos.
>>>>>>>
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> pgbr-geral mailing list
>>>>>>>> pgbr-geral@listas.postgresql.org.br
>>>>>>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> []s
>>>>>>> --
>>>>>>> JotaComm
>>>>>>> http://jotacomm.wordpress.com
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> pgbr-geral mailing list
>>>>>>> pgbr-geral@listas.postgresql.org.br
>>>>>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>>>>>
>>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> pgbr-geral mailing list
>>>>>> pgbr-geral@listas.postgresql.org.br
>>>>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>>>>
>>>>>>
>>>>>
>>>>> []s
>>>>> --
>>>>> JotaComm
>>>>> http://jotacomm.wordpress.com
>>>>>
>>>>> _______________________________________________
>>>>> pgbr-geral mailing list
>>>>> pgbr-geral@listas.postgresql.org.br
>>>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> pgbr-geral mailing list
>>>> pgbr-geral@listas.postgresql.org.br
>>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>>
>>>>
>>>
>>> []s
>>> --
>>> JotaComm
>>> http://jotacomm.wordpress.com
>>>
>>> _______________________________________________
>>> pgbr-geral mailing list
>>> pgbr-geral@listas.postgresql.org.br
>>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>>
>>>
>>
>> _______________________________________________
>> pgbr-geral mailing list
>> pgbr-geral@listas.postgresql.org.br
>> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>>
>>
>
> []s
> --
> JotaComm
> http://jotacomm.wordpress.com
>
> _______________________________________________
> pgbr-geral mailing list
> pgbr-geral@listas.postgresql.org.br
> https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral
>
>
_______________________________________________
pgbr-geral mailing list
pgbr-geral@listas.postgresql.org.br
https://listas.postgresql.org.br/cgi-bin/mailman/listinfo/pgbr-geral

Responder a