trigger to distribute the data in other tables (by conditions)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sentenza
    New Member
    • May 2010
    • 7

    trigger to distribute the data in other tables (by conditions)

    Here again with other trigger:

    the tables structure of the database:
    "database_t " (master),
    "azienda_vallon e" (SLAVE1),
    "azienda_energy " (SLAVE2 )
    ....
    all tables have identical structure.

    I want to create a trigger that on updates of table "database_t " I update the fields in the table "azienda_vallon e" and on insert in table "database_t " I insert rows in the table "azienda_vallon e" on one condition: If on updated or inserted row the column intestatario_pr atica = 'AZ. AGR.LA VALLONE' the line goes in table "azienda_vallon e" or if = 'AZ. AGR.LA ENERGY' the line goes "azienda_energy " (this second part not yet implemented in trigger).

    Follows my attempt:

    Code:
    CREATE OR REPLACE FUNCTION trig_agg_tab_aziende()
      RETURNS trigger AS
    $BODY$
    	BEGIN
    		IF (TG_OP = 'UPDATE') THEN
    	UPDATE azienda_vallone SET (sigla_comune, foglio, mappale, livello, the_geom, proprieta, 
           denominazione, titolo_disponibilita, titolo_possesso, sigla_provincia, 
           provincia, comune, porzione, sup_catastale, qualita_catastale, 
           vulnerabilita, sup_rilevata, sup_divieto, note_divieto, sup_spandibile, 
           coltura_pac, nome_suolo, num_suolo, fascia_altimetrica, area_omogenea, 
           appezzamento, codice_pua, particella_soppressa, superficie_calcolata, 
           zona_prg, articolo_prg, coltura_pan_09, coltura_pan_10, coltura_pan_11, intestatario_pratica, nome_tabella) = (
           NEW.sigla_comune, NEW.foglio, NEW.mappale, NEW.livello, NEW.the_geom, NEW.proprieta, 
           NEW.denominazione, NEW.titolo_disponibilita, NEW.titolo_possesso, NEW.sigla_provincia, 
           NEW.provincia, NEW.comune, NEW.porzione, NEW.sup_catastale, NEW.qualita_catastale, 
           NEW.vulnerabilita, NEW.sup_rilevata, NEW.sup_divieto, NEW.note_divieto, NEW.sup_spandibile, 
           NEW.coltura_pac, NEW.nome_suolo, NEW.num_suolo, NEW.fascia_altimetrica, NEW.area_omogenea, 
           NEW.appezzamento, NEW.codice_pua, NEW.particella_soppressa, NEW.superficie_calcolata, 
           NEW.zona_prg, NEW.articolo_prg, NEW.coltura_pan_09, NEW.coltura_pan_10, NEW.coltura_pan_11, NEW.intestatario_pratica, 
           NEW.nome_tabella)
    	FROM database_t
    	WHERE NEW.intestatario_pratica = 'AZ. AGR.LA VALLONE';
    		ELSIF (TG_OP = 'INSERT') THEN
    	INSERT INTO azienda_vallone SELECT 
           NEW.sigla_comune, NEW.foglio, NEW.mappale, NEW.livello, NEW.the_geom, NEW.proprieta, 
           NEW.denominazione, NEW.titolo_disponibilita, NEW.titolo_possesso, NEW.sigla_provincia, 
           NEW.provincia, NEW.comune, NEW.porzione, NEW.sup_catastale, NEW.qualita_catastale, 
           NEW.vulnerabilita, NEW.sup_rilevata, NEW.sup_divieto, NEW.note_divieto, NEW.sup_spandibile, 
           NEW.coltura_pac, NEW.nome_suolo, NEW.num_suolo, NEW.fascia_altimetrica, NEW.area_omogenea, 
           NEW.appezzamento, NEW.codice_pua, NEW.particella_soppressa, NEW.superficie_calcolata, 
           NEW.zona_prg, NEW.articolo_prg, NEW.coltura_pan_09, NEW.coltura_pan_10, NEW.coltura_pan_11, NEW.intestatario_pratica, 
           NEW.nome_tabella
    	FROM database_t
    	WHERE NEW.intestatario_pratica = 'AZ. AGR.LA VALLONE';
    		END IF;
    	RETURN NEW;
    	END;
    $BODY$
      LANGUAGE 'plpgsql' VOLATILE
      COST 100;
    ALTER FUNCTION trig_agg_tab_aziende() OWNER TO postgres;
    that function return no error but on insert or update nothing happens.

    thanks to all who help me!!
    Sentenza
    (I hope I have properly tagged the code)
  • rski
    Recognized Expert Contributor
    • Dec 2006
    • 700

    #2
    And where is trigger definition? You didn't put it here.

    Comment

    • sentenza
      New Member
      • May 2010
      • 7

      #3
      Thanks for the answer rski

      here the definition:

      Code:
      CREATE TRIGGER agg_tab_aziende 
      AFTER INSERT OR UPDATE 
      ON database_t
      FOR EACH ROW EXECUTE PROCEDURE trig_agg_tab_aziende();
      last night I tried again with the table "azienda_vallon e" partially populated and does not work correctly. if there are 5 rows of data and I make a change in "database_t ", the trigger overwrites all 5 lines of "azienda_vallon e" with the same line changed in "database_t ".

      I therefore have missed the basic concept ... but I do not understand!

      Thanks again
      sentenza

      Comment

      • rski
        Recognized Expert Contributor
        • Dec 2006
        • 700

        #4
        Why do you use
        UPDATE .... FROM t_database
        and
        INSERT ... FROM t_database
        You only should update NEW values in table azienda_vallone
        or insert NEW values into that table.
        NEW's are not column names but column values.

        Comment

        • sentenza
          New Member
          • May 2010
          • 7

          #5
          so i must correct the trigger deleting:

          "FROM database_t" ?

          and delete NEW.* from:

          WHERE NEW.intestatari o_pratica = 'AZ. AGR.LA VALLONE'

          then i must use:

          WHERE intestatario_pr atica = 'AZ. AGR.LA VALLONE'; ?


          I'll try tonight.

          thank you very much
          Sentenza

          Comment

          • sentenza
            New Member
            • May 2010
            • 7

            #6
            hi there
            Yesterday I tried, but does not work. when running the update, it write the updated row in all rows of the table azienda_vallone , entirely overwrite all existing rows.
            the guide says to mention in UPDATE only the columns that are modified but in this case is impossible to know first what columns will be modified.

            Sentenza

            Comment

            Working...