Transaction rowcount

  • The only errors encounted would be if you didn't have the database and table already created as the script relies on them existing.

    There's nothing wrong with SELECT @@ROWCOUNT -- a comment

  • Ok, but i thought there was a missing blank between "rowcount" and "--"...

    EuG

  • I may be getting confused with what you're saying..

    But all of this works fine

    select @@rowcount-- check this one out

    select @@rowcount -- check this one out

    select @@rowcount -- check this one out

    select @@rowcount

    -- check this one out

    These would not work

    select @@rowcount- - check this one out

    select rowcount -- check this one out

    select @@rowcount -

    - check this one out

    select @@rowcount -- check

    this one out

    We've a policy at work to use /* */ for comments where possible in case peeps run comments over a line... but it's also a pain in the backside if you want to comment out a block with these already in.

    What I've found a bit weird is, if you run SELECT @@ROWCOUNT by itself in a completely fresh window then you get 1 returned... slightly odd behaviour? For example run the below script, I've used different commenting in case of line wrapping.

    SELECT @@ROWCOUNT /* Will return 1, even though we've not previousiy run a statement */

    SELECT 1 WHERE 2 = 1 /* Will an empty record set */

    SELECT @@ROWCOUNT /* Will return 0 as expected as the previous statement returned no rows */

    SELECT @@ROWCOUNT /* Will return 1 as the previous statement returned a row i.e of 0 */

  • Nice question.

  • Fozzie (8/5/2010)


    What I've found a bit weird is, if you run SELECT @@ROWCOUNT by itself in a completely fresh window then you get 1 returned... slightly odd behaviour?

    I think that there was a QotD about this in the past and it generated a lot of posts due to this behaviour. It is actually by design, and it all depends on what happens when you open first window. For example, if you open SSMS, open new window then @@rowcount is 1, but if you after opening new window go after database list dropdown and pick your database then select @@rowcount returns 0 simply because clicking in the dropdown silently issued use [your_db_name]; prior to you running select @@rowcount. Whatever settings are silently ran by SSMS depends on things such as your user options, database options, SSMS version etc. The bottom line is that when you open SSMS and do see select @@rowcount returns 1 out of the bat then it simply means that the latest silently executed for you by SSMS script affected one record.

    Oleg

  • Ok, i didn't run the script of the question, but i thought the blank between end of line and "--" was mandatory.

    Thanks anyway for your answer and explanation.

    EuG

  • Oleg,

    Thanks for the heads up and clear explanation, does make a lot of sense. Will probably remember that one for a long time. 🙂

    Eugene,

    Whilst it's not neccessary to have a space between the code and the comment, it's certainly nicer on the eye. The code highlighter on the forum makes it look wrong as it only identifies whole words.

    Generally I'd have the spaces, as though I could write this:

    SELECT 1 FROM[dbo].

    It's not as well written, from my opinion as - plus you're less likely to get into trouble...

    SELECT 1 FROM [dbo].

  • I agree Fozzie !

  • nice tidy question with no tricks. clear explanation.

    Tom

Viewing 9 posts - 16 through 23 (of 23 total)

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