# Related Sites > SQL Course >  trigger to validate insert and update

## mrE

I need to validate the insertion and modification of accounting accounts.

An account has nature "D" or "A" then his subaccount need to be of the same nature.   (rule of Basic account someone toll me) 

Some users has been doing this... bad. Example:
Account   Nature  AcumTo
1000      A       null    - ok
1000 001  A       1000    - ok
1000 002  D       1000    - wrong!

¿How Can I use a trigger to validate the insertion or modification of account "1000 002", so the users dont save this. ?

Thanks in advance.

----------


## Claire

You can define instead of trigger for insert


Create TRIGGER tr_test on basetable
INSTEAD OF Insert
AS
if Condition ---
begin 
Insert Into BaseTable select Col1,Col2,Col3.. from Inserted
end

You could also try to define constraint or bind rule.All depends on which one is more efficient

----------


## mrE

Thanks for your answer. yes triggers are very usefull when they validate insert-update. Like this case.
This was my solution:

CREATE TRIGGER ValidaNaturalezaCta ON [dbo].[CuentaContable] 
FOR INSERT, UPDATE
AS
DECLARE @Naturaleza VARCHAR(1),
        @NaturalezaIns VARCHAR(1)
IF (SELECT Isnull(AcumulaA,'') FROM inserted)<>''
BEGIN
	SELECT @NaturalezaIns = Naturaleza FROM Inserted
	SELECT @Naturaleza = Naturaleza FROM CuentaContable WHERE Clave in (SELECT AcumulaA FROM Inserted)	
	IF @Naturaleza <> @NaturalezaIns
	BEGIN
		RAISERROR ('(Trigger) Men cannot have a gorilla son. The same rule apply here.', 16, 1)
		ROLLBACK TRANSACTION
	END
END

Thanks again for your advise.

----------

