• sjimmo (4/14/2010)


    Oleg,

    insert into #n

    select

    top 3 row_number() over (order by [object_id])

    from sys.objects;

    is not the same as

    insert #n values (1)

    insert #n values (2)

    insert #n values (3)

    Should you check the @@rowcount after performing your select, it will return 3, meaning that there were 3 rows affected by your SQL call.

    In the individual inserts,it will return a 1 after the last insert command.

    Yes, this is precisely what I tried to point out. In the original script the last insert as well as any insert before that set the @@rowcount to 1, and then the line

    set @result = cast (@@rowcount as varchar);

    did 2 things (set the variable and then reset the @@rowcount back to 1. So I changed the insert simply to reveal this behavior. The set-based insert set the @@rowcount to 3 and the line set @result = cast (@@rowcount as varchar); sets the @@rowcount to one as a result of the variable set.

    Oleg