Results 1 to 3 of 3

Thread: trigger to validate insert and update

  1. #1
    Join Date
    May 2004
    Posts
    2

    Question trigger to validate insert and update

    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.

  2. #2
    Join Date
    Mar 2003
    Location
    NJ
    Posts
    201
    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

  3. #3
    Join Date
    May 2004
    Posts
    2

    Thumbs up Thank you!

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •