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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to check for a never populated table on sql server 2005

How to check for a never populated table on sql server 2005

Scheduled Pinned Locked Moved Database
databasesql-servercomsysadminalgorithms
14 Posts 5 Posters 1 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.
  • D David Mujica

    Why don't you use a "select count(*)" statement to determine if the table has any rows in it ? I must be missing something. :confused:

    R Offline
    R Offline
    Roberto Mazzone
    wrote on last edited by
    #4

    Ok, I try to explain better. I want to predict what will be the next identity value for a table. If a table have 1 row inserted and deleted, the table is empty and populated. I this case IDENT_CURRENT return 1 and the next id value will be 2. Correct. If a table is empty and never populated IDENT_CURRENT return 1 again (it's a bug) and the next id value will be 1. In this particular circumstance i can't predict the next id value and i would like to kown a way to check if a table was populated or not. "Select count(*)" don't solve the problem. Anyway many thanks!

    1 Reply Last reply
    0
    • L leckey 0

      If your identity field is called something like myIndex, why not just do something like DECLARE myCounter INT SET @myCounter = SELECT MAX(myIndex) FROM myTableName DELETE FROM myTableName WHERE myIndex = @myCounter

      Back in the blog beatch! http://CraptasticNation.blogspot.com/[^]

      R Offline
      R Offline
      Roberto Mazzone
      wrote on last edited by
      #5

      Thanks for replay leckey. I try to explain better. I want to predict what will be the next identity value for a table. If a table has 1 row inserted and deleted, the table is empty and populated. I this case IDENT_CURRENT return 1 and the next id value will be 2. Correct. If a table is empty and never populated IDENT_CURRENT return 1 again (it's a bug) and the next id value will be 1. In this particular circumstance i can't predict the next id value and i would like to know a way to check if a table was populated to achieve my goal. With your code I can't predict the next identity (autonumber) value in the scenario above explained. Anyway, thanks again.

      1 Reply Last reply
      0
      • R Roberto Mazzone

        I use IDENT_CURRENT to predict the next identity on a table (I know I have to be careful). IDENT_CURRENT return the SEED value (1) for a never populated table (it's a bug...https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=297781[^]) and the same value for an empty table with 1 rows inserted and deleted. so when IDENT_CURRENT return 1 I don't know if the new identity value will be 1 or 2. I have to exec:

        DBCC CHECKIDENT ('table_name', RESEED, 1) --if a table was empty and never populated

        DBCC CHECKIDENT ('table_name', RESEED, 0) --if a table was empty and just populated

        in this way IDENT_CURRENT will return always 1. I'm searching for a more elegant solution than, for an empty table, insert a dummy row, get the IDENT_CURRENT and delete the dummy row! Many thanks to all

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #6

        IDENT_CURRENT is not a reliable way of predicting the next identity. It can give you a probable value, but you can never be sure of what the next value will be in practice because of concurrent inserts, rollbacks, etc. It will only give you a "best guess" at what the next identity value is likely to be. The bottom line is that you can't predict the next identity, not reliably. If you need to work around this bug, you could use an insert trigger to update a counter value in another table. That way you can tell how many inserts there have been on your main table. If the counter is 0, your next identity is 1. Otherwise, go and look at IDENT_CURRENT. It's a bit clunky but it's better than doing an insert then a delete. If you really need 100% predictability of the next identity value, you will probably be better off not using IDENTITY but rolling your own solution (it's not hard - people used to do it all the time before IDENTITY came on the scene). That way you can be completely in control of what value gets allocated to each new insert.

        D 1 Reply Last reply
        0
        • D David Skelly

          IDENT_CURRENT is not a reliable way of predicting the next identity. It can give you a probable value, but you can never be sure of what the next value will be in practice because of concurrent inserts, rollbacks, etc. It will only give you a "best guess" at what the next identity value is likely to be. The bottom line is that you can't predict the next identity, not reliably. If you need to work around this bug, you could use an insert trigger to update a counter value in another table. That way you can tell how many inserts there have been on your main table. If the counter is 0, your next identity is 1. Otherwise, go and look at IDENT_CURRENT. It's a bit clunky but it's better than doing an insert then a delete. If you really need 100% predictability of the next identity value, you will probably be better off not using IDENTITY but rolling your own solution (it's not hard - people used to do it all the time before IDENTITY came on the scene). That way you can be completely in control of what value gets allocated to each new insert.

          D Offline
          D Offline
          David Mujica
          wrote on last edited by
          #7

          I agree 100% that the most reliable way to solve this would be to build your own sequence generator where you are in control of the "identity" values. Wrap the "getNextSequence" function call and your insert record logic in a transaction to guarantee things stay in synch. Before you go and build something, I would re-think the algorithm that needs to know the "next" identity. Is this really necessary ? The whole idea behind an identity column is that it provides a unique number for the row and you shouldn't care what that value is until the row is actually created. Give it some thought. I like to keep things simple. :)

          R 1 Reply Last reply
          0
          • R Roberto Mazzone

            I use IDENT_CURRENT to predict the next identity on a table (I know I have to be careful). IDENT_CURRENT return the SEED value (1) for a never populated table (it's a bug...https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=297781[^]) and the same value for an empty table with 1 rows inserted and deleted. so when IDENT_CURRENT return 1 I don't know if the new identity value will be 1 or 2. I have to exec:

            DBCC CHECKIDENT ('table_name', RESEED, 1) --if a table was empty and never populated

            DBCC CHECKIDENT ('table_name', RESEED, 0) --if a table was empty and just populated

            in this way IDENT_CURRENT will return always 1. I'm searching for a more elegant solution than, for an empty table, insert a dummy row, get the IDENT_CURRENT and delete the dummy row! Many thanks to all

            M Offline
            M Offline
            Mike Ellison
            wrote on last edited by
            #8

            Why do you need to predict the next identity value on a table? I'm just curious.

            www.MishaInTheCloud.com

            R 1 Reply Last reply
            0
            • D David Mujica

              I agree 100% that the most reliable way to solve this would be to build your own sequence generator where you are in control of the "identity" values. Wrap the "getNextSequence" function call and your insert record logic in a transaction to guarantee things stay in synch. Before you go and build something, I would re-think the algorithm that needs to know the "next" identity. Is this really necessary ? The whole idea behind an identity column is that it provides a unique number for the row and you shouldn't care what that value is until the row is actually created. Give it some thought. I like to keep things simple. :)

              R Offline
              R Offline
              Roberto Mazzone
              wrote on last edited by
              #9

              First of all many thanks to David Skelly and David Mujica for the work-around about the IDENT_CURRENT bug. Second, i'm an sql script beginner. I know that about IDENT_CURRENT Microsoft report "Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENTITY_INCR because of insertions performed by other sessions." The job I have to do is to move rows from DB_SRC to DB_DST having the same schema. DB_DST can be empty or not. --------------------------- TAB. tA --------------------------- a_id int identity not null, b_id int not null ---------------------------

              alter table tA
              add constraint tB_tA_FK1 foreign key (b_id)
              references tB (b_id)

              --------------------------- TAB. tB --------------------------- b_id int identity not null --------------------------- this was my first implementation...

              USE [DB_SRC]

              CREATE TABLE #corr_id
              (
              b_id_src INT NULL,
              b_id_dst INT NULL
              }

              DECLARE @IdNext AS INT
              SET @IdNext = IDENT_CURRENT('[DB_DST].dbo.tB')

              -- Set the correspondence
              INSERT INTO #corr_id (b_id_src, b_id_dst)
              SELECT b_id, ROW_NUMBER() OVER (order by b_id) + @IdNext
              FROM tB WHERE b_id IN .....code depending on user choise

              -- Use the correspondence
              INSERT INTO [DB_DST].dbo.tA
              SELECT #corr_id.b_id_src FROM tA INNER JOIN #corr_id ON tA.b_id = #corr_id.b_id_src

              but the IDENT_CURRENT bug (explained in previous post) occured and than...

              USE [DB_DST]
              ALTER TABLE tB ADD id_src INT NULL
              GO

              USE [DB_SRC]

              -- Set the correspondence
              INSERT INTO [DB_DST].dbo.tB (id_src)
              SELECT b_id FROM tB WHERE b_id IN .....code depending on user choise

              -- Use the correspondence
              INSERT INTO [DB_DST].dbo.tA
              SELECT tBDST.b_id FROM tA INNER JOIN [DB_DST].dbo.tB AS tBDST
              ON tA.b_id = tBDST.id_src

              USE [DB_DST]
              ALTER TABLE tB DROP COLUMN id_src
              GO

              BTW, turning back to the post object, is there a built-in function or sp to know if a table was never populated?

              modified on Wednesday, August 5, 2009 8:26 AM

              D 1 Reply Last reply
              0
              • M Mike Ellison

                Why do you need to predict the next identity value on a table? I'm just curious.

                www.MishaInTheCloud.com

                R Offline
                R Offline
                Roberto Mazzone
                wrote on last edited by
                #10

                Mike, anyway i'm an sql script beginner. I know that about IDENT_CURRENT Microsoft report "Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENTITY_INCR because of insertions performed by other sessions." The job I have to do is to move rows from DB_SRC to DB_DST having the same schema. DB_DST can be empty or not. ---------------------------- TAB. tA ---------------------------- a_id int identity not null, b_id int not null ----------------------------

                alter table tA
                add constraint tB_tA_FK1 foreign key (b_id)
                references tB (b_id)

                ---------------------------- TAB. tB ---------------------------- b_id int identity not null ---------------------------- this was my first implementation...

                USE [DB_SRC]

                CREATE TABLE #corr_id
                (
                b_id_src INT NULL,
                b_id_dst INT NULL
                }

                DECLARE @IdNext AS INT
                SET @IdNext = IDENT_CURRENT('[DB_DST].dbo.tB')

                -- Set the correspondence
                INSERT INTO #corr_id (b_id_src, b_id_dst)
                SELECT b_id, ROW_NUMBER() OVER (order by b_id) + @IdNext
                FROM tB WHERE b_id IN .....code depending on user choise

                -- Use the correspondence
                INSERT INTO [DB_DST].dbo.tA
                SELECT #corr_id.b_id_src FROM tA INNER JOIN #corr_id ON tA.b_id = #corr_id.b_id_src

                but the IDENT_CURRENT bug (explained in previous post) occured and than...

                USE [DB_DST]
                ALTER TABLE tB ADD id_src INT NULL
                GO

                USE [DB_SRC]

                -- Set the correspondence
                INSERT INTO [DB_DST].dbo.tB (id_src)
                SELECT b_id FROM tB WHERE b_id IN .....code depending on user choise

                -- Use the correspondence
                INSERT INTO [DB_DST].dbo.tA
                SELECT tBDST.b_id FROM tA INNER JOIN [DB_DST].dbo.tB AS tBDST
                ON tA.b_id = tBDST.id_src

                USE [DB_DST]
                ALTER TABLE tB DROP COLUMN id_src
                GO

                BTW, turning back to the post object, is there a built-in function or sp to know if a table was never populated?

                modified on Wednesday, August 5, 2009 8:36 AM

                M 1 Reply Last reply
                0
                • R Roberto Mazzone

                  First of all many thanks to David Skelly and David Mujica for the work-around about the IDENT_CURRENT bug. Second, i'm an sql script beginner. I know that about IDENT_CURRENT Microsoft report "Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENTITY_INCR because of insertions performed by other sessions." The job I have to do is to move rows from DB_SRC to DB_DST having the same schema. DB_DST can be empty or not. --------------------------- TAB. tA --------------------------- a_id int identity not null, b_id int not null ---------------------------

                  alter table tA
                  add constraint tB_tA_FK1 foreign key (b_id)
                  references tB (b_id)

                  --------------------------- TAB. tB --------------------------- b_id int identity not null --------------------------- this was my first implementation...

                  USE [DB_SRC]

                  CREATE TABLE #corr_id
                  (
                  b_id_src INT NULL,
                  b_id_dst INT NULL
                  }

                  DECLARE @IdNext AS INT
                  SET @IdNext = IDENT_CURRENT('[DB_DST].dbo.tB')

                  -- Set the correspondence
                  INSERT INTO #corr_id (b_id_src, b_id_dst)
                  SELECT b_id, ROW_NUMBER() OVER (order by b_id) + @IdNext
                  FROM tB WHERE b_id IN .....code depending on user choise

                  -- Use the correspondence
                  INSERT INTO [DB_DST].dbo.tA
                  SELECT #corr_id.b_id_src FROM tA INNER JOIN #corr_id ON tA.b_id = #corr_id.b_id_src

                  but the IDENT_CURRENT bug (explained in previous post) occured and than...

                  USE [DB_DST]
                  ALTER TABLE tB ADD id_src INT NULL
                  GO

                  USE [DB_SRC]

                  -- Set the correspondence
                  INSERT INTO [DB_DST].dbo.tB (id_src)
                  SELECT b_id FROM tB WHERE b_id IN .....code depending on user choise

                  -- Use the correspondence
                  INSERT INTO [DB_DST].dbo.tA
                  SELECT tBDST.b_id FROM tA INNER JOIN [DB_DST].dbo.tB AS tBDST
                  ON tA.b_id = tBDST.id_src

                  USE [DB_DST]
                  ALTER TABLE tB DROP COLUMN id_src
                  GO

                  BTW, turning back to the post object, is there a built-in function or sp to know if a table was never populated?

                  modified on Wednesday, August 5, 2009 8:26 AM

                  D Offline
                  D Offline
                  David Mujica
                  wrote on last edited by
                  #11

                  Is DB_DST a read-only database ? Will records every be inserted into this database ? If not, then maybe for table, tB, you can drop the "Identity" qualifier and just do a brute force copy from tA to tB. This will guarantee that the ID values are the same between the two tables. David

                  R 1 Reply Last reply
                  0
                  • R Roberto Mazzone

                    Mike, anyway i'm an sql script beginner. I know that about IDENT_CURRENT Microsoft report "Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENTITY_INCR because of insertions performed by other sessions." The job I have to do is to move rows from DB_SRC to DB_DST having the same schema. DB_DST can be empty or not. ---------------------------- TAB. tA ---------------------------- a_id int identity not null, b_id int not null ----------------------------

                    alter table tA
                    add constraint tB_tA_FK1 foreign key (b_id)
                    references tB (b_id)

                    ---------------------------- TAB. tB ---------------------------- b_id int identity not null ---------------------------- this was my first implementation...

                    USE [DB_SRC]

                    CREATE TABLE #corr_id
                    (
                    b_id_src INT NULL,
                    b_id_dst INT NULL
                    }

                    DECLARE @IdNext AS INT
                    SET @IdNext = IDENT_CURRENT('[DB_DST].dbo.tB')

                    -- Set the correspondence
                    INSERT INTO #corr_id (b_id_src, b_id_dst)
                    SELECT b_id, ROW_NUMBER() OVER (order by b_id) + @IdNext
                    FROM tB WHERE b_id IN .....code depending on user choise

                    -- Use the correspondence
                    INSERT INTO [DB_DST].dbo.tA
                    SELECT #corr_id.b_id_src FROM tA INNER JOIN #corr_id ON tA.b_id = #corr_id.b_id_src

                    but the IDENT_CURRENT bug (explained in previous post) occured and than...

                    USE [DB_DST]
                    ALTER TABLE tB ADD id_src INT NULL
                    GO

                    USE [DB_SRC]

                    -- Set the correspondence
                    INSERT INTO [DB_DST].dbo.tB (id_src)
                    SELECT b_id FROM tB WHERE b_id IN .....code depending on user choise

                    -- Use the correspondence
                    INSERT INTO [DB_DST].dbo.tA
                    SELECT tBDST.b_id FROM tA INNER JOIN [DB_DST].dbo.tB AS tBDST
                    ON tA.b_id = tBDST.id_src

                    USE [DB_DST]
                    ALTER TABLE tB DROP COLUMN id_src
                    GO

                    BTW, turning back to the post object, is there a built-in function or sp to know if a table was never populated?

                    modified on Wednesday, August 5, 2009 8:36 AM

                    M Offline
                    M Offline
                    Mike Ellison
                    wrote on last edited by
                    #12

                    It may be that my intuition is off, but I am getting the sense that you are making something much more difficult than it needs to be. If I understand your post, you are simply moving data from a source database to a destination? And your destination needs to have an identity column? Why not just let it auto-increment itself the normal way? I think I must be missing something here.

                    www.MishaInTheCloud.com

                    R 1 Reply Last reply
                    0
                    • M Mike Ellison

                      It may be that my intuition is off, but I am getting the sense that you are making something much more difficult than it needs to be. If I understand your post, you are simply moving data from a source database to a destination? And your destination needs to have an identity column? Why not just let it auto-increment itself the normal way? I think I must be missing something here.

                      www.MishaInTheCloud.com

                      R Offline
                      R Offline
                      Roberto Mazzone
                      wrote on last edited by
                      #13

                      Mike, I have to preserve the referential integrity of this constraint.

                      alter table tA
                      add constraint tB_tA_FK1 foreign key (b_id)
                      references tB (b_id)

                      Situation before moving data from DB_SRC to DB_DST on DB_SRC.tA a_id b_id 1 100 on DB_SRC.tB b_id 100 on DB_DST.tA a_id b_id 1 200 on DB_DST.tB b_id 200 when i insert data from DB_SRC.tB to DB_DST.tB. b_id became 201 and when i insert data from DB_SRC.tA to DB_DST.tA DB_DST.tA.b_id have to became 201. To achieve this goal i used the IDENT_CURRENT for DB_DST.tB (200) plus ROW_NUMBER (the rank progressive, from 1 to n) as previously reported. I solved adding an extracolumn DB_DST.tB.id_src, than on DB_DST.tB b_id id_src 200 201 100 in this way i have the right correspondence to do the work as explained in the previous post. Anyway many thanks for your interest.

                      1 Reply Last reply
                      0
                      • D David Mujica

                        Is DB_DST a read-only database ? Will records every be inserted into this database ? If not, then maybe for table, tB, you can drop the "Identity" qualifier and just do a brute force copy from tA to tB. This will guarantee that the ID values are the same between the two tables. David

                        R Offline
                        R Offline
                        Roberto Mazzone
                        wrote on last edited by
                        #14

                        David, see my reply to Mike...

                        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