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. How do you increment a temporary table?

How do you increment a temporary table?

Scheduled Pinned Locked Moved Database
helpquestion
4 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.
  • D Offline
    D Offline
    DFlat4Now
    wrote on last edited by
    #1

    Select * Into #BC From dfCert -- Creates master Certification table -> #BC Declare @BoardCert Char(3) Set @BoardCert = 'BCn' Declare @Counter int Set @Counter = 1 While @Counter < 10 Begin Select Biog_Nbr, Max(Biog_Cert_ID) as aMaxBiogCertID, Max(Cert_Year) as aMaxCertYear Into #tblBoardCert From #BC Group By Biog_Nbr Delete From #BC From #BC Inner Join #tblBoardCert On #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Where #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Set @BoardCert = Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') Select * Into #@BoardCert --<< Here's the problem. I need this to be #BC1 then BC2, etc up to BC9 From #tblBoardCert Drop Table #tblBoardCert Set @Counter = @Counter + 1 Print 'The counter has just increased to ' + Cast(@Counter as Char) End

    N M 2 Replies Last reply
    0
    • D DFlat4Now

      Select * Into #BC From dfCert -- Creates master Certification table -> #BC Declare @BoardCert Char(3) Set @BoardCert = 'BCn' Declare @Counter int Set @Counter = 1 While @Counter < 10 Begin Select Biog_Nbr, Max(Biog_Cert_ID) as aMaxBiogCertID, Max(Cert_Year) as aMaxCertYear Into #tblBoardCert From #BC Group By Biog_Nbr Delete From #BC From #BC Inner Join #tblBoardCert On #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Where #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Set @BoardCert = Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') Select * Into #@BoardCert --<< Here's the problem. I need this to be #BC1 then BC2, etc up to BC9 From #tblBoardCert Drop Table #tblBoardCert Set @Counter = @Counter + 1 Print 'The counter has just increased to ' + Cast(@Counter as Char) End

      N Offline
      N Offline
      Niladri_Biswas
      wrote on last edited by
      #2

      I guess there is some problem in the counter placement. You are dropping the #TtblBoardCert and then you are incrementing. Drop Table #tblBoardCert Set @Counter = @Counter + 1 So before the first increment itself the table is dropped. Also I have a doubt in this line Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') Check if it is giving you the correct result or not. Hope this helps. :)

      Niladri Biswas

      D 1 Reply Last reply
      0
      • N Niladri_Biswas

        I guess there is some problem in the counter placement. You are dropping the #TtblBoardCert and then you are incrementing. Drop Table #tblBoardCert Set @Counter = @Counter + 1 So before the first increment itself the table is dropped. Also I have a doubt in this line Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') Check if it is giving you the correct result or not. Hope this helps. :)

        Niladri Biswas

        D Offline
        D Offline
        DFlat4Now
        wrote on last edited by
        #3

        I drop the table because it needs to be rebuild on each cycle. The "Max" records are removed on the first cycle and then I pull the next set of "Max" records for the second cycle. This goes on until #BC is empty. Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') actually works. The real problem is the following: Select * Into #@BoardCert From #tblBoardCert It doesn't see #@BoardCert as #BC1 then #BC2 etc., it only sees it as #@BoardCert and so it errors because it thinks it's already written to #@BoardCert on the first cycle.

        1 Reply Last reply
        0
        • D DFlat4Now

          Select * Into #BC From dfCert -- Creates master Certification table -> #BC Declare @BoardCert Char(3) Set @BoardCert = 'BCn' Declare @Counter int Set @Counter = 1 While @Counter < 10 Begin Select Biog_Nbr, Max(Biog_Cert_ID) as aMaxBiogCertID, Max(Cert_Year) as aMaxCertYear Into #tblBoardCert From #BC Group By Biog_Nbr Delete From #BC From #BC Inner Join #tblBoardCert On #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Where #BC.Biog_Nbr = #tblBoardCert.Biog_Nbr and #BC.Biog_Cert_ID = #tblBoardCert.AMaxBiogCertID and #BC.Cert_Year = #tblBoardCert.aMaxCertYear Set @BoardCert = Replace(Left(@BoardCert,2)+Cast(@Counter as Char),' ','') Select * Into #@BoardCert --<< Here's the problem. I need this to be #BC1 then BC2, etc up to BC9 From #tblBoardCert Drop Table #tblBoardCert Set @Counter = @Counter + 1 Print 'The counter has just increased to ' + Cast(@Counter as Char) End

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          This just sounds weird, without going through the details of your script I think you can achieve this in 1 table by including a setID. If you need to create an incremented value over a set you can use row_number (over setID). Creating and dropping temp tables is just bad design.

          Never underestimate the power of human stupidity RAH

          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