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. TOP 1 in select statement with different results.

TOP 1 in select statement with different results.

Scheduled Pinned Locked Moved Database
databasedebuggingquestion
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.
  • V Offline
    V Offline
    VK Cadec
    wrote on last edited by
    #1

    Hi, Are there any issues using TOP clause in temp tables in stored procedure? We are experiencing some difficulties for which we are unable to debug. We are receiving two sets of different results, for same input parameters. When we run stored proc the first time versus when it is run 5 minutes later, the results are not consistent. We are assuming using the TOP 1 clause may have some issues. Thanks for your time.

    A M 2 Replies Last reply
    0
    • V VK Cadec

      Hi, Are there any issues using TOP clause in temp tables in stored procedure? We are experiencing some difficulties for which we are unable to debug. We are receiving two sets of different results, for same input parameters. When we run stored proc the first time versus when it is run 5 minutes later, the results are not consistent. We are assuming using the TOP 1 clause may have some issues. Thanks for your time.

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Do you have an "order by" clause on your select statement?

      V 1 Reply Last reply
      0
      • A andyharman

        Do you have an "order by" clause on your select statement?

        V Offline
        V Offline
        VK Cadec
        wrote on last edited by
        #3

        Yes.

        A 1 Reply Last reply
        0
        • V VK Cadec

          Yes.

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          Can you post the code for the stored procedure to this forum? There are no issues with doing a "top 1" on temporary tables - apart from the fact the SQL-Server may possibly use a different query-plan each time you run it. However, your "order by" clause should have fixed that. Is it possible that your temporary table somehow contains different data for each of the runs? I have had this caused by an update or delete statement using different query plans for different runs.

          V 1 Reply Last reply
          0
          • A andyharman

            Can you post the code for the stored procedure to this forum? There are no issues with doing a "top 1" on temporary tables - apart from the fact the SQL-Server may possibly use a different query-plan each time you run it. However, your "order by" clause should have fixed that. Is it possible that your temporary table somehow contains different data for each of the runs? I have had this caused by an update or delete statement using different query plans for different runs.

            V Offline
            V Offline
            VK Cadec
            wrote on last edited by
            #5

            Here you go. -------------- select top 1 @lastdrive = t1.datetimestamp from #e t1 inner join #r r1 on r1.routestart <= t1.datetimestamp and r1.routeend >= t1.datetimestamp where t1.eventnum = 1108 and t1.datetimestamp >= @stopstart and t1.datetimestamp <= @stopend and r1.routedatekey = @routedatekey and t1.driverkey = r1.driverkey and r1.driverkey = @driverkey order by t1.datetimestamp desc -------------- This is run within a cursor. Also - does it make a difference if we used something like -- select @lastdrive = max (t1.datetimestamp) -- instead? Thanks. Vani

            A 1 Reply Last reply
            0
            • V VK Cadec

              Here you go. -------------- select top 1 @lastdrive = t1.datetimestamp from #e t1 inner join #r r1 on r1.routestart <= t1.datetimestamp and r1.routeend >= t1.datetimestamp where t1.eventnum = 1108 and t1.datetimestamp >= @stopstart and t1.datetimestamp <= @stopend and r1.routedatekey = @routedatekey and t1.driverkey = r1.driverkey and r1.driverkey = @driverkey order by t1.datetimestamp desc -------------- This is run within a cursor. Also - does it make a difference if we used something like -- select @lastdrive = max (t1.datetimestamp) -- instead? Thanks. Vani

              A Offline
              A Offline
              andyharman
              wrote on last edited by
              #6

              I would use the @lastdrive = max(t1.datetimestamp) method. It may allow you to remove the cursor from your code.

              1 Reply Last reply
              0
              • V VK Cadec

                Hi, Are there any issues using TOP clause in temp tables in stored procedure? We are experiencing some difficulties for which we are unable to debug. We are receiving two sets of different results, for same input parameters. When we run stored proc the first time versus when it is run 5 minutes later, the results are not consistent. We are assuming using the TOP 1 clause may have some issues. Thanks for your time.

                M Offline
                M Offline
                Mike Dimmick
                wrote on last edited by
                #7

                You need to use ## for a global temporary table. A temporary table whose name begins with a single # is a local temporary table and is local to the connection. SQL Server 2005 documentation says: "There are two types of temporary tables: local and global. They differ from each other in their names, their visibility, and their availability. Local temporary tables have a single number sign (#) as the first character of their names; they are visible only to the current connection for the user, and they are deleted when the user disconnects from the instance of SQL Server. Global temporary tables have two number signs (##) as the first characters of their names; they are visible to any user after they are created, and they are deleted when all users referencing the table disconnect from the instance of SQL Server."

                Stability. What an interesting concept. -- Chris Maunder

                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