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. Cannot use temporary table twice

Cannot use temporary table twice

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
11 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.
  • M Offline
    M Offline
    MarcelloTurnbull
    wrote on last edited by
    #1

    I get the next error message from MS SQL 2000: "There is already an object named '#Teste' in the database."

    DECLARE @x int

    SET @x = 5

    IF @x > 1

    SELECT IDField1
    INTO #Teste
    FROM Table1

    ELSE

    SELECT IDField2
    INTO #Teste
    FROM Table1

    (...)

    The snippet code above is just an example. I cannot use another temporary table name because the following code must reference #Teste only. Do you know a solution ? Thanks Marcello Turnbull

    E W 2 Replies Last reply
    0
    • M MarcelloTurnbull

      I get the next error message from MS SQL 2000: "There is already an object named '#Teste' in the database."

      DECLARE @x int

      SET @x = 5

      IF @x > 1

      SELECT IDField1
      INTO #Teste
      FROM Table1

      ELSE

      SELECT IDField2
      INTO #Teste
      FROM Table1

      (...)

      The snippet code above is just an example. I cannot use another temporary table name because the following code must reference #Teste only. Do you know a solution ? Thanks Marcello Turnbull

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Where is your create and drop statement for the temp table? Last I heard a temp table was supposed to be local to the scope of the procedure and it was an error to not global to the entire db. Also, you can look into the possibility of using the new table variable type in SQL Server.

      Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

      1 Reply Last reply
      0
      • M MarcelloTurnbull

        I get the next error message from MS SQL 2000: "There is already an object named '#Teste' in the database."

        DECLARE @x int

        SET @x = 5

        IF @x > 1

        SELECT IDField1
        INTO #Teste
        FROM Table1

        ELSE

        SELECT IDField2
        INTO #Teste
        FROM Table1

        (...)

        The snippet code above is just an example. I cannot use another temporary table name because the following code must reference #Teste only. Do you know a solution ? Thanks Marcello Turnbull

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        You seem to use a local temporary table. When such table is created it exists until you either: - disconnect from the database - drop the table using DROP TABLE command So now you create the table in your SELECT ... INTO statement and if you don't drop it somewhere later, you encounter that error when you run the code block the second time.

        The need to optimize rises from a bad design.My articles[^]

        M 1 Reply Last reply
        0
        • W Wendelius

          You seem to use a local temporary table. When such table is created it exists until you either: - disconnect from the database - drop the table using DROP TABLE command So now you create the table in your SELECT ... INTO statement and if you don't drop it somewhere later, you encounter that error when you run the code block the second time.

          The need to optimize rises from a bad design.My articles[^]

          M Offline
          M Offline
          MarcelloTurnbull
          wrote on last edited by
          #4

          I agree there is no DROP statement but it is not enough to fix the problem. Thanks Marcello Turnbull

          W 1 Reply Last reply
          0
          • M MarcelloTurnbull

            I agree there is no DROP statement but it is not enough to fix the problem. Thanks Marcello Turnbull

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Don't quite understand. Your problem is that you try to create a table that already exists. So you must either drop it or modify your code so that you don't create the table if it already exists.

            The need to optimize rises from a bad design.My articles[^]

            M 1 Reply Last reply
            0
            • W Wendelius

              Don't quite understand. Your problem is that you try to create a table that already exists. So you must either drop it or modify your code so that you don't create the table if it already exists.

              The need to optimize rises from a bad design.My articles[^]

              M Offline
              M Offline
              MarcelloTurnbull
              wrote on last edited by
              #6

              After you answer I added the DROP TABLE in the beginning of the code but it has not worked. I must not modify the code following the snippet. Thank you Marcello Turnbull

              W 1 Reply Last reply
              0
              • M MarcelloTurnbull

                After you answer I added the DROP TABLE in the beginning of the code but it has not worked. I must not modify the code following the snippet. Thank you Marcello Turnbull

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                Could you post the whole code? Also do you receive the same error or something else?

                The need to optimize rises from a bad design.My articles[^]

                M 1 Reply Last reply
                0
                • W Wendelius

                  Could you post the whole code? Also do you receive the same error or something else?

                  The need to optimize rises from a bad design.My articles[^]

                  M Offline
                  M Offline
                  MarcelloTurnbull
                  wrote on last edited by
                  #8

                  DROP TABLE #Teste

                  DECLARE @x int
                  SET @x = 5

                  IF @x > 1
                  SELECT IDField1
                  INTO #Teste
                  FROM Table1

                  ELSE

                  SELECT IDField2
                  INTO #Teste
                  FROM Table1

                  (...)

                  Buddy, the error message is the same. Thanks Marcello Turnbull

                  W 1 Reply Last reply
                  0
                  • M MarcelloTurnbull

                    DROP TABLE #Teste

                    DECLARE @x int
                    SET @x = 5

                    IF @x > 1
                    SELECT IDField1
                    INTO #Teste
                    FROM Table1

                    ELSE

                    SELECT IDField2
                    INTO #Teste
                    FROM Table1

                    (...)

                    Buddy, the error message is the same. Thanks Marcello Turnbull

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #9

                    That's odd. For some reason the parser interpets that the table is going to be created twice (of course that's not gonna happen since you have IF ... ELSE structure). However, could you use the following:

                    DROP TABLE #Teste;
                    CREATE TABLE #Teste (
                    IDField1 int
                    );
                    DECLARE @x int;
                    SET @x = 5;
                    IF (@x > 1)
                    BEGIN
                    INSERT INTO #Teste
                    SELECT IDField1
                    FROM Table1;
                    END;
                    ELSE
                    BEGIN
                    INSERT INTO #Teste
                    SELECT IDField1
                    FROM Table1;
                    END;

                    The need to optimize rises from a bad design.My articles[^]

                    M 1 Reply Last reply
                    0
                    • W Wendelius

                      That's odd. For some reason the parser interpets that the table is going to be created twice (of course that's not gonna happen since you have IF ... ELSE structure). However, could you use the following:

                      DROP TABLE #Teste;
                      CREATE TABLE #Teste (
                      IDField1 int
                      );
                      DECLARE @x int;
                      SET @x = 5;
                      IF (@x > 1)
                      BEGIN
                      INSERT INTO #Teste
                      SELECT IDField1
                      FROM Table1;
                      END;
                      ELSE
                      BEGIN
                      INSERT INTO #Teste
                      SELECT IDField1
                      FROM Table1;
                      END;

                      The need to optimize rises from a bad design.My articles[^]

                      M Offline
                      M Offline
                      MarcelloTurnbull
                      wrote on last edited by
                      #10

                      Buddy I apologize for I could not reply yesterday but it was holiday here ;-) Snapshots[^] It worked so thank you very much Marcello Turnbull

                      W 1 Reply Last reply
                      0
                      • M MarcelloTurnbull

                        Buddy I apologize for I could not reply yesterday but it was holiday here ;-) Snapshots[^] It worked so thank you very much Marcello Turnbull

                        W Offline
                        W Offline
                        Wendelius
                        wrote on last edited by
                        #11

                        You're welcome :)

                        The need to optimize rises from a bad design.My articles[^]

                        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