• mister.magoo (8/6/2013)


    Sean Lange (8/6/2013)


    Lowell (8/6/2013)


    no, you should say "Because @@rowcount is affected by any commands in the session, it's my practice to re-select from the table in case there was a trigger that might returna different @@rowcount i might not expect."

    just blame force of habit to resolve rare edge cases like that.

    I read this post and thought the same thing. So I decided I should test my theory. It seems that @@rowcount returns the correct number at least in this simple test.

    create table RowCountTest

    (

    id int

    )

    go

    create trigger RowCountTestTrigger on RowCountTest after insert as

    select top 1000 *

    into #Something

    from Tally

    go

    insert RowCountTest

    select 1

    select @@ROWCOUNT

    If this was a QOTD I would have answered that the value of @@rowcount would be 1000. Interesting. Maybe I will have to do a little more digging and submit as a qotd soon.

    An INSTEAD OF trigger can definitely result in @@rowcount being different to SELECT count(x) FROM ...

    Same result changing this to an instead of trigger.

    alter trigger RowCountTestTrigger on RowCountTest

    instead of insert as

    select top 1000 *

    into #Something

    from Tally

    go

    insert RowCountTest

    select 1

    select @@ROWCOUNT

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/