Trigger Fails to Work

  • I am working with an application that builds a group of records in our retail data base. I wanted to assign a taxid to the group of records that are automatically generated by the application. I thought that the trigger below would do it but now I think that the system may not be just simply inserting the group of records, but inserting and then updating them. Anyway, the taxid is always zero after this executes.

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    ALTER TRIGGER [dbo].[tr_TaxID]

    ON [dbo].[Item] after INSERT, UPDATE AS

    begin

    declare @rows as int, @id as int, @taxid as int

    declare i_csr cursor for

    select Id from INSERTED

    open i_csr

    select @rows = @@cursor_rows

    if @rows <> 0

    begin

    fetch next from i_csr

    into @id

    while @@fetch_status = 0

    begin

    select @taxid = 1

    update item set taxid = @taxid whereitem.id = @id

    fetch next from i_csr

    into @id

    End

    End

    Close i_csr

    deallocate i_csr

    End

  • All that's going to do is set taxid to 1 for all records inserted. You don't need to use a cursor, either - it's a bad idea from a performance point of view and especially so in a trigger. What are you trying to do - set the same taxid for all rows inserted in one batch, or set a totally unique id?

    John

  • That is performance killer trigger!

    You should avoid using cursors when coding in T-SQL. Especially for this sort of tasks.

    We will need a bit more details from you about what are you trying to do.

    In your "trigger" @taxid is only set once to 1, it doesn't look right to me...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I want to set taxid = 1 to all new records in this table. I also want the user to be able to change the taxid to something else if thats what they want to do. Some items are taxable others are not.

    The taxid remains 0 after this trigger executes. Not one record has a 1 in it.

  • How do I update the inserted records without using a cursor? I thought that I had to use a cursor to reference the INSERTED records.

  • ALTER TRIGGER [dbo].[tr_TaxID]

    ON [dbo].[Item] after INSERT, UPDATE AS

    UPDATE item SET taxid = 1 WHERE item.id IN (SELECT id FROM inserted);

    Done.

    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
  • Something like this?

    UPDATE item

    SET taxid = 1

    WHERE id IN (SELECT id FROM Inserted)

    You can't - or at least shouldn't - have user input in a trigger. If users need to update the value, they should do it in a separate operation.

    John

  • You should be able to update all rows at once, like so:

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    ALTER TRIGGER [dbo].[tr_TaxID]

    ON [dbo].[Item]

    after INSERT, UPDATE AS

    UPDATE [dbo].[Item]

    SET

    taxid = 1

    FROM [dbo].[Item] item

    INNER JOIN inserted i ON

    i.id = item.id

    GO

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • jean 93062 (3/12/2012)


    I want to set taxid = 1 to all new records in this table. I also want the user to be able to change the taxid to something else if thats what they want to do. Some items are taxable others are not.

    The taxid remains 0 after this trigger executes. Not one record has a 1 in it.

    You can do this even without using a trigger. Define your column "id" with a default value of 1. If the user wants to change it, he can.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • GilaMonster (3/12/2012)


    ALTER TRIGGER [dbo].[tr_TaxID]

    ON [dbo].[Item] after INSERT, UPDATE AS

    UPDATE item SET taxid = 1 WHERE item.id IN (SELECT id FROM inserted);

    Done.

    Just small note:

    If you do it as above, even when anyone will try to change taxid to something else than 1, you will still end up with 1 in the table, as your trigger is defined for INSERT and UPDATE.

    Actually, you can change this trigger to be for INSERT only.

    Or, do it as it should be done:

    Define your taxid column to be not-nullable and with default value of 1.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Thank you. Much simpler.

  • Right, the user would be using the application to make a change and that function is not intended to be in the trigger.

  • I'll try that because I think the application is doing something else and overrulling the effects of the trigger. Even the simpler trigger is still producing no "1". Remains zero.

  • Yes, thank you. I will remove the "UPDATED". It was there for testing.

  • jean 93062 (3/12/2012)


    I'll try that because I think the application is doing something else and overrulling the effects of the trigger. Even the simpler trigger is still producing no "1". Remains zero.

    The trigger fires after anything that the app would do, so there's no way the app can override it. Only way that trigger can not fire is if the app is using some form of bulk insert as those don't fire triggers by default.

    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 15 posts - 1 through 15 (of 17 total)

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