Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SET NOCOUNT

SET NOCOUNT

Scheduled Pinned Locked Moved Database
databasesql-serversysadmintutorialquestion
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 3484936

    When I Put SET NOCOUNT ON before sql statement its giving -1 rows effected in sql server 2005. But in my code i need rows effected 0 . How to do that ?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    You told the server not to count, so it returns -1. Why do you "need" a count of 0?

    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

    1 Reply Last reply
    0
    • U User 3484936

      When I Put SET NOCOUNT ON before sql statement its giving -1 rows effected in sql server 2005. But in my code i need rows effected 0 . How to do that ?

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #3

      That's an old concept for returning a value when actually no value can be returned (e.g. due to an error). Here, you told the server that it must not count the rows affected. When you now ask "how many rows were affected" it cannot return the "correct" value, you ought to expect an exception, but for compatibility with legacy applications, it returns a number. Any whole number from 0 to inifinite would pretend to be the correct result. Hence, the server must not return any value in that range. And therefore, it returns -1: it is not possible that a negative number of rows was affected. Get used to that old concept, you may encounter it more often.

      U 1 Reply Last reply
      0
      • B Bernhard Hiller

        That's an old concept for returning a value when actually no value can be returned (e.g. due to an error). Here, you told the server that it must not count the rows affected. When you now ask "how many rows were affected" it cannot return the "correct" value, you ought to expect an exception, but for compatibility with legacy applications, it returns a number. Any whole number from 0 to inifinite would pretend to be the correct result. Hence, the server must not return any value in that range. And therefore, it returns -1: it is not possible that a negative number of rows was affected. Get used to that old concept, you may encounter it more often.

        U Offline
        U Offline
        User 3484936
        wrote on last edited by
        #4

        Dim Ssql Dim dbstatus Dim rs '************* Ssql = " Select * from User_Access A Where A.User_pwd ='1234' " set rs = Server.CreateObject("ADODB.RecordSet") rs = DB.execute(Ssql,dbstatus) Response.Write(dbstatus) If dbstatus <> 0 Then 'Error code here Else 'code here END IF '************* Above is the code working ok . Actually this is written in dll. Now code will give error if "dbstatus" is other than 0 . Here I'm getting -1 always in my environment.

        L 1 Reply Last reply
        0
        • U User 3484936

          Dim Ssql Dim dbstatus Dim rs '************* Ssql = " Select * from User_Access A Where A.User_pwd ='1234' " set rs = Server.CreateObject("ADODB.RecordSet") rs = DB.execute(Ssql,dbstatus) Response.Write(dbstatus) If dbstatus <> 0 Then 'Error code here Else 'code here END IF '************* Above is the code working ok . Actually this is written in dll. Now code will give error if "dbstatus" is other than 0 . Here I'm getting -1 always in my environment.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          ..and what's the problem with changing that code to reflect reality? There aren't 0 records affected, but an "uncounted" number;

          If dbstatus <> -1 Then
          'Error code here
          Else
          'code here
          END IF

          Then again, your If-Else wouldn't be very valuable, since it will always return -1 (unless an exception occurs or you raiserror)

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          U 1 Reply Last reply
          0
          • U User 3484936

            When I Put SET NOCOUNT ON before sql statement its giving -1 rows effected in sql server 2005. But in my code i need rows effected 0 . How to do that ?

            _ Offline
            _ Offline
            _Amy
            wrote on last edited by
            #6

            Hi, Because you are telling to server, "don't count the numbers of rows affected". If you need the numbers of rows affected then try setting SET NOCOUNT OFF. All the bset.

            Read the article "Table Valued Parameters". --Amit

            U 1 Reply Last reply
            0
            • L Lost User

              ..and what's the problem with changing that code to reflect reality? There aren't 0 records affected, but an "uncounted" number;

              If dbstatus <> -1 Then
              'Error code here
              Else
              'code here
              END IF

              Then again, your If-Else wouldn't be very valuable, since it will always return -1 (unless an exception occurs or you raiserror)

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              U Offline
              U Offline
              User 3484936
              wrote on last edited by
              #7

              I can't do that ...

              L 1 Reply Last reply
              0
              • _ _Amy

                Hi, Because you are telling to server, "don't count the numbers of rows affected". If you need the numbers of rows affected then try setting SET NOCOUNT OFF. All the bset.

                Read the article "Table Valued Parameters". --Amit

                U Offline
                U Offline
                User 3484936
                wrote on last edited by
                #8

                SET NOCOUNT OFF is working for insert delete update but for select statement it is giving -1 always .

                L _ 2 Replies Last reply
                0
                • U User 3484936

                  SET NOCOUNT OFF is working for insert delete update but for select statement it is giving -1 always .

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  Member 3487632 wrote:

                  for select statement it is giving -1 always

                  What a shocker. It is what the doc[^] says: For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command... For all other types of statements, the return value is -1 :|

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  U 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Member 3487632 wrote:

                    for select statement it is giving -1 always

                    What a shocker. It is what the doc[^] says: For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command... For all other types of statements, the return value is -1 :|

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    U Offline
                    U Offline
                    User 3484936
                    wrote on last edited by
                    #10

                    YES..it is

                    1 Reply Last reply
                    0
                    • U User 3484936

                      When I Put SET NOCOUNT ON before sql statement its giving -1 rows effected in sql server 2005. But in my code i need rows effected 0 . How to do that ?

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #11

                      Based on your other responses. You have the following code 1. SQL 2. Calling code. In a previous statement it appears that you cannot modify 2. So that means you MUST modify 1 such that it returns the value that you want. Thus for example you can structure a stored proc such that if there are no results, which you check for, then you return zero. (That means you write the code to do just that.)

                      1 Reply Last reply
                      0
                      • U User 3484936

                        I can't do that ...

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        ..add a SELECT 1 WHERE 1 = 0 to your Sql-statement. IIRC and you execute a batch, only the affected rows of the last statement are returned.

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        1 Reply Last reply
                        0
                        • U User 3484936

                          SET NOCOUNT OFF is working for insert delete update but for select statement it is giving -1 always .

                          _ Offline
                          _ Offline
                          _Amy
                          wrote on last edited by
                          #13

                          Member 3487632 wrote:

                          select statement it is giving -1 always

                          Why you are checking the SET NOCOUNT in the case of select? Instead of that you can check your result-set rows.. Check your result-set rows for the number of rows selected.. :)

                          Read the article "Table Valued Parameters". --Amit

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups