<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Jorge,<br>
    <br>
    Obrigado por ajudar.<br>
    <br>
    Eu quero que sempre que insiramos novos registos na tabela elementos
    este update dispare:<br>
    <br>
    UPDATE elementos SET n_rua = (SELECT n_rua FROM ruas ORDER BY
    ST_Distance(ruas.the_geom, elementos.the_geom) ASC LIMIT 1)<br>
    <br>
    O update &eacute; sobre a mesma tabela. Como formulo a fun&ccedil;&atilde;o?<br>
    <br>
    obrigado<br>
    <pre class="moz-signature" cols="72">Com os melhores cumprimentos,

Pedro Costa
Ge&oacute;grafo
Especializado em Sistemas de Informa&ccedil;&atilde;o Geogr&aacute;fica e Ordenamento do Territ&oacute;rio

</pre>
    <br>
    Em 14-11-2011 11:46, Jorge Penedo escreveu:
    <blockquote
cite="mid:CAMWe5LrCmmtZfq478bngrNU=momxvFxe+0tNK-TP+V2aGMQbYQ@mail.gmail.com"
      type="cite"><br>
      <br>
      <div class="gmail_quote">No dia 14 de Novembro de 2011 11:44,
        Jorge Penedo <span dir="ltr">&lt;<a moz-do-not-send="true"
            href="mailto:jorge.penedo@gmail.com">jorge.penedo@gmail.com</a>&gt;</span>
        escreveu:<br>
        <blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px
          0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
          <div>&nbsp;</div>
          <div>O after / ac&ccedil;&atilde;o s&oacute; &eacute; disparado ap&oacute;s&nbsp; a ocurr&ecirc;ncia. Por
            exemplo ao inserir numa tabela, o trigger s&oacute; dispara depois
            dos dados estarem armazenados.</div>
          <div>&nbsp;</div>
          <div>J&aacute; no before, o trigger dispara antes dos dados
            estarem&nbsp;guardados.</div>
          <div>&nbsp;</div>
          <div>Se percebi quer fazer um insert numa tabela, e aos
            regsitos novos pretende fazer uma actualiza&ccedil;&atilde;o noutra tabela
            &eacute; isso?</div>
          <div>&nbsp;</div>
          <div>&nbsp;</div>
          <div>por exemplo, se tiver uma tabela de nome testes, e sempre
            que ocorra uma inser&ccedil;&atilde;o&nbsp; quer</div>
          <div>que essa mesma informa&ccedil;&atilde;o seja inserida na tabela
            trecebe.</div>
          <div>&nbsp;</div>
          <div>deve criar uma fun&ccedil;&atilde;o para trigger, do g&eacute;nero;</div>
          <div>&nbsp;</div>
          <div>"</div>
          <div>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">CREATE OR
                REPLACE FUNCTION insert_testes()</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>RETURNS
                trigger AS</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">$BODY$</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">begin</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>insert into trecebe (id_novo,nome)</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>values (<a moz-do-not-send="true"
                  href="http://new.id/" target="_blank">new.id</a>,
                new.nome);</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>delete from testes where id = <a
                  moz-do-not-send="true" href="http://new.id/"
                  target="_blank">new.id</a>;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                </span>return NULL;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">end</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">$BODY$</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>LANGUAGE
                'plpgsql' VOLATILE</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>COST
                100;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">ALTER
                FUNCTION insert_testes() OWNER TO postgres;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">&nbsp;"</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"></span>&nbsp;</p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">e um trigger
                do tipo:&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"></span>&nbsp;</p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">"&nbsp;</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt">CREATE
                TRIGGER altera_tabela</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>AFTER
                INSERT</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>ON
                testes</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>FOR
                EACH ROW</span></p>
            <p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"><span
                style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"><span>&nbsp; </span>EXECUTE
                PROCEDURE insert_testes();</span></p>
          </div>
          <div>"</div>
          <div>&nbsp;</div>
          <div>&nbsp;</div>
          <div>Notas: na fun&ccedil;&atilde;o existe o prefixo new que se refer aos
            valores inseridos. </div>
          <div>Caso fosse uma ac&ccedil;&atilde;o em tivesse alterado dados ( update
            ou delete) teria a possibilidade</div>
          <div>de gerir os novos valores "new" e oa antigos "old".</div>
          <div>&nbsp;</div>
          <div>&nbsp;</div>
          <div>Para criar estas fun&ccedil;&otilde;es e trigger pode recorrer ao
            pgadmin, onde &eacute; um pouco mais intuitivo.</div>
          <div>&nbsp;</div>
          <div>Agora se pretende inserir um registo e fazer um update de
            seguida, ao mesmo registo, das duas uma</div>
          <div>ou ao fazer o insert insere logo os dados na totalidade,
            ou na fun&ccedil;&atilde;o do trigger inclui as</div>
          <div>clausulas de actualiza&ccedil;&atilde;o.</div>
          <div>&nbsp;</div>
          <div>&nbsp;</div>
          <div>exemplo de update:</div>
        </blockquote>
        <div>&nbsp;</div>
        <div>&nbsp;</div>
        <blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px
          0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
          <div>&nbsp;update tabela set campo = novo_valor where id = <a
              moz-do-not-send="true" href="http://new.id">new.id</a></div>
        </blockquote>
        <div>&nbsp;</div>
        <blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px
          0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
          <div>&nbsp;</div>
          <div><br>
            <br>
            &nbsp;</div>
          <div class="gmail_quote">No dia 14 de Novembro de 2011 11:28,
            Pedro Costa <span dir="ltr">&lt;<a moz-do-not-send="true"
                href="mailto:pedrocostaarma@sapo.pt" target="_blank">pedrocostaarma@sapo.pt</a>&gt;</span>
            escreveu:
            <div>
              <div class="h5"><br>
                <blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN:
                  0px 0px 0px 0.8ex; PADDING-LEFT: 1ex"
                  class="gmail_quote">
                  <div bgcolor="#FFFFFF" text="#000000">postgres, ent&atilde;o
                    para actualizar apenas os novos tenho de utilizar o
                    update e o before?<br>
                    <br>
                    Obrigado pela ajuda<br>
                    <pre cols="72">Com os melhores cumprimentos,

Pedro Costa
Ge&oacute;grafo
Especializado em Sistemas de Informa&ccedil;&atilde;o Geogr&aacute;fica e Ordenamento do Territ&oacute;rio

</pre>
                    <br>
                    Em 14-11-2011 11:18, Jorge Penedo escreveu:
                    <div>
                      <div>
                        <blockquote type="cite">
                          <div>Quando se define um trigger&nbsp; &eacute; indicado o
                            evento&nbsp; after/before insert/delete/update</div>
                          <div>o que pretende &eacute; algo do tipo</div>
                          <div>&nbsp;</div>
                          <div>after insert ou before insert</div>
                          <div>&nbsp;</div>
                          <div>onde &eacute; poss&iacute;vel detectar todos os registo
                            novos, inseridos numa tabela</div>
                          <div>&nbsp;</div>
                          <div>como n&atilde;o me indicou se est&aacute; a trabalhar
                            com sqlserver, postgres, oracle ou outro
                            SGBD n&atilde;o poderei</div>
                          <div>ser mais especifico.</div>
                          <div>&nbsp;</div>
                          <div>Qual o SGBD&nbsp; que est&aacute;&nbsp; a usar?</div>
                          <div>&nbsp;</div>
                          <div>Cumprimentos</div>
                          <div>&nbsp;</div>
                          <div>Jorge Penedo</div>
                          <div>&nbsp;<br>
                            <br>
                          </div>
                          <div class="gmail_quote">No dia 14 de Novembro
                            de 2011 11:05, Pedro Costa <span dir="ltr">&lt;<a
                                moz-do-not-send="true"
                                href="mailto:pedrocostaarma@sapo.pt"
                                target="_blank">pedrocostaarma@sapo.pt</a>&gt;</span>
                            escreveu:<br>
                            <blockquote style="BORDER-LEFT: #ccc 1px
                              solid; MARGIN: 0px 0px 0px 0.8ex;
                              PADDING-LEFT: 1ex" class="gmail_quote">Pessoal
                              algu&eacute;m podia ajudar a formular um trigger
                              que dispare quando inserimos novos
                              registos numa tabela?<br>
                              Queria que executasse um update mas apenas
                              nos registos novos inseridos no momento,
                              n&atilde;o em todos.<br>
                              Algu&eacute;m pode ajudar?<br>
                              <br>
                              Obrigado<span><font color="#888888"><br>
                                  <br>
                                  -- <br>
                                  Com os melhores cumprimentos,<br>
                                  <br>
                                  Pedro Costa<br>
                                  <br>
                                  <br>
_______________________________________________<br>
                                  Portugal mailing list<br>
                                  <a moz-do-not-send="true"
                                    href="mailto:Portugal@lists.osgeo.org"
                                    target="_blank">Portugal@lists.osgeo.org</a><br>
                                  <a moz-do-not-send="true"
                                    href="http://lists.osgeo.org/mailman/listinfo/portugal"
                                    target="_blank">http://lists.osgeo.org/mailman/listinfo/portugal</a><br>
                                </font></span></blockquote>
                          </div>
                          <br>
                          <br>
                          <fieldset></fieldset>
                          <br>
                          <pre>_______________________________________________
Portugal mailing list
<a moz-do-not-send="true" href="mailto:Portugal@lists.osgeo.org" target="_blank">Portugal@lists.osgeo.org</a>
<a moz-do-not-send="true" href="http://lists.osgeo.org/mailman/listinfo/portugal" target="_blank">http://lists.osgeo.org/mailman/listinfo/portugal</a>
</pre>
                        </blockquote>
                      </div>
                    </div>
                  </div>
                  <br>
                  _______________________________________________<br>
                  Portugal mailing list<br>
                  <a moz-do-not-send="true"
                    href="mailto:Portugal@lists.osgeo.org"
                    target="_blank">Portugal@lists.osgeo.org</a><br>
                  <a moz-do-not-send="true"
                    href="http://lists.osgeo.org/mailman/listinfo/portugal"
                    target="_blank">http://lists.osgeo.org/mailman/listinfo/portugal</a><br>
                  <br>
                </blockquote>
              </div>
            </div>
          </div>
          <br>
        </blockquote>
      </div>
      <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Portugal mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Portugal@lists.osgeo.org">Portugal@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/portugal">http://lists.osgeo.org/mailman/listinfo/portugal</a>
</pre>
    </blockquote>
  </body>
</html>