Logic problem in "SET [Quantity_Remaining] = [Quantity_Remaining] - 1"

  • briancampbellmcad (10/19/2012)


    IF (SELECT Transaction_Type FROM INSERTED) = 'From Bulk Assignment'

    That is a very common mistake in triggers.

    If 3 rows are inserted in a single operation, one has a transaction type of 'From Bulk Assignment' and the other two are some other type, that IF may return true (depends which row you get with that IF) and if if does, the update then runs on all 3 rows, not 1.

    The way you had it earlier with the WHERE in the update was more correct. Consider putting the WHERE in the subquery on inserted, not in an IF which will sooner or later do something you don't expect.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing post 46 (of 45 total)

You must be logged in to reply to this topic. Login to reply