Why do you have to SET NOCOUNT ON?

  • I've read that you should put SET NOCOUNT ON at the start of a stored procedure. What is NOCOUNT, and why does it have to be set to on in a procedure?

  • SET NOCOUNT ON stops SQL from sending back the progress messages generated by Transact-SQL statements that affect one or more rows.

    If you run your stored procedure without SET NOCOUNT ON you might see something like:

    1 records affected

    3 records affected

    ...

    SET NOCOUNT stops this.

    The reason it is useful - all those messages are sent back to the client, therefore generating network traffic.

    You may find that you need to SET NOCOUNT OFF before the final statement in your stored procedure if your client code needs to read the number of rows affected by your procedure.

    Regards

    Simon

    UK

  • ADO tends not to like it either - not sure its 100% consistent, but I've seen a lot of cases where it misinterprets the nocount message as a recordset with no cols and no rows.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 3 posts - 1 through 2 (of 2 total)

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