SQL Server generic trigger creation

696 Views Asked by At

I am trying to create a generic trigger in SQL Server which can copy all column data from Table A and insert them in corresponding fields in Table B.

There are few problems I am facing.

I need this copy to occur under three conditions : INSERT, DELETE and UPDATE.

  1. The trigger needs to be triggered after CUD operations. using AFTER throws SQL error saying ntext etc are not supported in inserted. How do I resolve this error?

  2. Instead of if used can work for INSERT but not for delete. Is there a way to do this for delete operations?

  3. Is there a way I can write a generic code inside the trigger which can work for all sorts of tables (we can assume that all the columns in table a exists in column b)

I am not well versed with triggers or for that matter DDL in SQL Server.

Appreciate if some can provide me some solutions.

Thanks Ben

1

There are 1 best solutions below

0
On

CREATE TRIGGER (Transact-SQL)

  1. Use nvarchar(max) instead of ntext.
  2. You can have an instead of trigger for delete.
  3. You can have one trigger that handles insert/update/delete for one table but you can not connect a trigger to more than one table.