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. Limits on SQL Statements

Limits on SQL Statements

Scheduled Pinned Locked Moved Database
helpc++databasesql-serversysadmin
7 Posts 3 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.
  • B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #1

    Hi, I wonder if there is any limitation concerning the length of a sql statement. I have the following situation: I need to update a single column of a lot of records in a table. Updating those 20K (20000) records one at the time consumes a lot of time. In order to speed things up I collect those queries to create one big SQL statement. Now the problem is the following whenever I place more that 455 update queries only the first 455 are executed. The others are neglected, because no error has happened (neighter in my try/catch block or in the SQL Server checked with the profiler). The size of the sql statement (455 queries) is about 20Kb in size. Any thoughts on this would be very appriciated BTW: I'm developing in VC++ 2005 on a SQL Server V2000 codito ergo sum

    D A 2 Replies Last reply
    0
    • B BadKarma

      Hi, I wonder if there is any limitation concerning the length of a sql statement. I have the following situation: I need to update a single column of a lot of records in a table. Updating those 20K (20000) records one at the time consumes a lot of time. In order to speed things up I collect those queries to create one big SQL statement. Now the problem is the following whenever I place more that 455 update queries only the first 455 are executed. The others are neglected, because no error has happened (neighter in my try/catch block or in the SQL Server checked with the profiler). The size of the sql statement (455 queries) is about 20Kb in size. Any thoughts on this would be very appriciated BTW: I'm developing in VC++ 2005 on a SQL Server V2000 codito ergo sum

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      BadKarma wrote:

      Updating those 20K (20000) records one at the time consumes a lot of time. In order to speed things up I collect those queries to create one big SQL statement. Now the problem is the following whenever I place more that 455 update queries only the first 455 are executed. The others are neglected, because no error has happened (neighter

      You did what??? :omg: You have a big problem in your design. Like you've found out, if one statement fails, you have to consider the entire batch a failure. You can't catch an error because nothing throws one. Best practice says that you call a stored procedure on the SQL Server to do the update for you and just pass the parameters to it, 20,000 times. The stored procedure can check to see if the update works and RAISEERROR if it failed for some reason. You can catch that error in your code. You also get the benefit of knowing exactly which one of these calls failed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      B 1 Reply Last reply
      0
      • B BadKarma

        Hi, I wonder if there is any limitation concerning the length of a sql statement. I have the following situation: I need to update a single column of a lot of records in a table. Updating those 20K (20000) records one at the time consumes a lot of time. In order to speed things up I collect those queries to create one big SQL statement. Now the problem is the following whenever I place more that 455 update queries only the first 455 are executed. The others are neglected, because no error has happened (neighter in my try/catch block or in the SQL Server checked with the profiler). The size of the sql statement (455 queries) is about 20Kb in size. Any thoughts on this would be very appriciated BTW: I'm developing in VC++ 2005 on a SQL Server V2000 codito ergo sum

        A Offline
        A Offline
        AntonGogolev
        wrote on last edited by
        #3

        SQL Server 2000 has a limit on the batch size, which is 65,536 times the network packet size, which is, by default, 4096 bytes, so what we have is somewhere around 256 Mb. So batch size is not an issue in your case. You may try issuing the same batch from Query Analyzer to see the results.

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          BadKarma wrote:

          Updating those 20K (20000) records one at the time consumes a lot of time. In order to speed things up I collect those queries to create one big SQL statement. Now the problem is the following whenever I place more that 455 update queries only the first 455 are executed. The others are neglected, because no error has happened (neighter

          You did what??? :omg: You have a big problem in your design. Like you've found out, if one statement fails, you have to consider the entire batch a failure. You can't catch an error because nothing throws one. Best practice says that you call a stored procedure on the SQL Server to do the update for you and just pass the parameters to it, 20,000 times. The stored procedure can check to see if the update works and RAISEERROR if it failed for some reason. You can catch that error in your code. You also get the benefit of knowing exactly which one of these calls failed. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          B Offline
          B Offline
          BadKarma
          wrote on last edited by
          #4

          Dave Kreskowiak wrote:

          if one statement fails, you have to consider the entire batch a failure

          I know for certain that all queries are valid. If I execute 400 of them in batch there is no problem, even 455 of them. Only when I exceed the 455 queries 'limit' in the batch, then the last are neglected. Due to what reason can a valid query be neglected ??? codito ergo sum

          D 1 Reply Last reply
          0
          • B BadKarma

            Dave Kreskowiak wrote:

            if one statement fails, you have to consider the entire batch a failure

            I know for certain that all queries are valid. If I execute 400 of them in batch there is no problem, even 455 of them. Only when I exceed the 455 queries 'limit' in the batch, then the last are neglected. Due to what reason can a valid query be neglected ??? codito ergo sum

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Because the query IS invalid, the last lines are being cut off. There is a limit to the size of a query string you can pass. I think it's 8192 bytes, but I can't find solid documentation on it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            B 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Because the query IS invalid, the last lines are being cut off. There is a limit to the size of a query string you can pass. I think it's 8192 bytes, but I can't find solid documentation on it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              First of all thanks for your time too reply. The queries i execute look like this:

              UPDATE MyTable SET MyField = 1 WHERE Data = 1;
              UPDATE MyTable SET MyField = 1 WHERE Data = 2;
              UPDATE MyTable SET MyField = 1 WHERE Data = 3;
              UPDATE MyTable SET MyField = 1 WHERE Data = 4;
              

              this goes on up until Data reaches 1000. So like you can see the size of the query is relative small not even 50 characters. The batch statement though, can reach the size of more the 20k of characters but shouldn't be a problem because the max bacth size is 65536 * network packet size (see response from Sl0n[^]) When I execute this batch to an limit of 455 queries, there is no problem. The size of this batch is over 8192 byte limit (its arround 18K). When I add more queries to the bacth every query past this 455 limit just doesn't get executed and the batch statement is returned as a success. So even though I stay well under the limits, those queries aren't executed ??? Or do I miss something? codito ergo sum

              D 1 Reply Last reply
              0
              • B BadKarma

                First of all thanks for your time too reply. The queries i execute look like this:

                UPDATE MyTable SET MyField = 1 WHERE Data = 1;
                UPDATE MyTable SET MyField = 1 WHERE Data = 2;
                UPDATE MyTable SET MyField = 1 WHERE Data = 3;
                UPDATE MyTable SET MyField = 1 WHERE Data = 4;
                

                this goes on up until Data reaches 1000. So like you can see the size of the query is relative small not even 50 characters. The batch statement though, can reach the size of more the 20k of characters but shouldn't be a problem because the max bacth size is 65536 * network packet size (see response from Sl0n[^]) When I execute this batch to an limit of 455 queries, there is no problem. The size of this batch is over 8192 byte limit (its arround 18K). When I add more queries to the bacth every query past this 455 limit just doesn't get executed and the batch statement is returned as a success. So even though I stay well under the limits, those queries aren't executed ??? Or do I miss something? codito ergo sum

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                BadKarma wrote:

                So like you can see the size of the query is relative small not even 50 characters. The batch statement though, can reach the size of more the 20k of characters but shouldn't be

                With emphasis on "shouldn't". It's not so much the size of the query in characters, but I'm thinking the number of statements in the batch might be your problem. It's not listed in the specs anywhere. There are other minor possibilities, but I've got no time to test them. Maximum statements cachable in a connection, limits on uncompiled vs. compiled code vs. batch code, ... What you're doing is just never done outside a stored procedure in the database (at least I've never seen it done), so data on your technique is very hard to come by. You can try the theory out by batching together statements 400 at a time. ORrrrrrr, you could just write the batch query using an SQL loop, since your data doesn't appear to change from iteration to iteration, just the WHERE clause changes. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -- modified at 11:46 Monday 30th January, 2006

                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