Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserted, Deleted tables in postgreSQL, like SQL Server?

I want to create a trigger after a inserted event, but I need the data that I inserted in order to register into a new table for my trigger in PostgreSQL

In SQL Server I capture these values from the Inserted or deleted pseudo tables but do these tables also exists in PostgreSQL? Or what can I do?

This is my trigger code

CREATE TRIGGER tri_compago
  AFTER INSERT
  ON matricula
  FOR EACH ROW
  EXECUTE PROCEDURE fn_insCompPago();

CREATE OR REPLACE FUNCTION fn_insCompPago()
  RETURNS trigger AS
$BODY$
  DECLARE
  BEGIN

   insert into compromisopago(codigotasa,descripcion,precio,fechavencimiento,codigomatricula)
   select codigotasa,descripcion,precio,fechavencimiento,i.codigo
   from programacionpago pp join inserted i on isnull(i.codigoconvenio,0) = isnull (pp.codigoconvenio,0)
   and pp.codigopresentacion = i.codigopresentacion
   where pp.vigencia = 1 and i.vigencia = 1;

   RETURN NULL;
  END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION fn_insCompPago()
  OWNER TO postgres;
like image 598
MitoCode Avatar asked May 14 '13 00:05

MitoCode


People also ask

How do we use inserted and deleted tables outside of trigger?

As per MSDN: DML trigger statements use two special tables: the deleted table and the inserted table. SQL Server automatically creates and manages these tables. You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for DML trigger actions.

What is the usage of inserted and deleted tables in the database trigger?

In DML triggers, the inserted and deleted tables are primarily used to perform the following: Extend referential integrity between tables. Insert or update data in base tables underlying a view. Test for errors and take action based on the error.

What is the purpose of the inserted and deleted tables in SQL Server?

Inserted and deleted are the magic tables in the SQL Server trigger that used to manage pre-updated and post updated row.

What are magic tables?

Magic tables are the temporary logical tables that are created by the SQL server whenever there are insertion or deletion or update( D.M.L) operations. The recently performed operation on the rows gets stored in magic tables automatically. These are not physical table but they are just temporary internal tables.


1 Answers

I have no idea how triggers work in SQL Server but in PostgreSQL, you use the OLD and NEW special variables:

NEW
Data type RECORD; variable holding the new database row for INSERT/UPDATE operations in row-level triggers. This variable is NULL in statement-level triggers and for DELETE operations.

OLD
Data type RECORD; variable holding the old database row for UPDATE/DELETE operations in row-level triggers. This variable is NULL in statement-level triggers and for INSERT operations.

So you probably want to look at NEW.codigo, NEW.codigoconvenio, NEW.codigopresentacion, and NEW.vigencia in your case. You'd probably replace the i.vigencia = 1 part of the WHERE clause with a simple IF i.vigencia = 1 conditional as well.

like image 71
mu is too short Avatar answered Oct 12 '22 22:10

mu is too short