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. select top query

select top query

Scheduled Pinned Locked Moved Database
databasehelptutorial
11 Posts 6 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
    brettokumar
    wrote on last edited by
    #1

    hi i am created a store procedure like this for select top list user definlly create procedure ss(@s int) as select top @s sno from summa1 order by newid() it can not create and display error how to create

    with regards, bretto

    C A S 3 Replies Last reply
    0
    • B brettokumar

      hi i am created a store procedure like this for select top list user definlly create procedure ss(@s int) as select top @s sno from summa1 order by newid() it can not create and display error how to create

      with regards, bretto

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      In you are using SQL Server 2000 then this is not possible.

      Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

      S 1 Reply Last reply
      0
      • B brettokumar

        hi i am created a store procedure like this for select top list user definlly create procedure ss(@s int) as select top @s sno from summa1 order by newid() it can not create and display error how to create

        with regards, bretto

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

        I suspect you are in sql server 2000, which will not accept a parameter for the top clause. In 2005 it would work. You need to create and execute the sql dynamically for it to work in 2000.

        Bob Ashfield Consultants Ltd

        1 Reply Last reply
        0
        • B brettokumar

          hi i am created a store procedure like this for select top list user definlly create procedure ss(@s int) as select top @s sno from summa1 order by newid() it can not create and display error how to create

          with regards, bretto

          S Offline
          S Offline
          SomeGuyThatIsMe
          wrote on last edited by
          #4

          in sql server 2000 try create procedure ss(@s int) as SET ROWCOUNT @s select sno from summa1 order by newid SET ROWCOUNT 0 ROWCOUNT should turn pink..i havnt used it in a while so i may have typed it wrong. I do not believe that Rowcount is available in sql server 2k5, it was replaced with something but i dont remember what. EDIT: ROWCOUNT is the correct one..@@ROWCOUNT wont work. you could also use dynamic sql like EXEC 'SELECT TOP ' + CAST(@s AS CHAR) + ' sno FROM summa1 ORDER BY newid'

          Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

          modified on Friday, June 27, 2008 4:16 PM

          A 1 Reply Last reply
          0
          • C Colin Angus Mackay

            In you are using SQL Server 2000 then this is not possible.

            Recent blog posts: * Introduction to LINQ to XML (Part 1) - (Part 2) - (part 3) My website | Blog

            S Offline
            S Offline
            SomeGuyThatIsMe
            wrote on last edited by
            #5

            You can use SET @@rowcount or create procedure ss(@s int) as EXEC 'select top ' + CAST(@s AS CHAR) + ' sno from summa1 order by newid()'

            Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

            P 1 Reply Last reply
            0
            • S SomeGuyThatIsMe

              in sql server 2000 try create procedure ss(@s int) as SET ROWCOUNT @s select sno from summa1 order by newid SET ROWCOUNT 0 ROWCOUNT should turn pink..i havnt used it in a while so i may have typed it wrong. I do not believe that Rowcount is available in sql server 2k5, it was replaced with something but i dont remember what. EDIT: ROWCOUNT is the correct one..@@ROWCOUNT wont work. you could also use dynamic sql like EXEC 'SELECT TOP ' + CAST(@s AS CHAR) + ' sno FROM summa1 ORDER BY newid'

              Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

              modified on Friday, June 27, 2008 4:16 PM

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

              I don't think this would work, I'm pretty sure @@rowcount is read only. I don't have sql server 2000 installed, but I have never seen @@rowcount being set, only ever read. What I think is meant is set rowcount, but this cannot be set to a parameter, so you are back to dynamic sql

              SomeGuyThatIsMe wrote:

              I do not believe that Rowcount is available in sql server 2k5,

              Both rowcount (to set the number of rows returned) and @@rowcount (the number of rows affected) are both still alive and kicking in SQL Server 2005

              Bob Ashfield Consultants Ltd

              S 1 Reply Last reply
              0
              • A Ashfield

                I don't think this would work, I'm pretty sure @@rowcount is read only. I don't have sql server 2000 installed, but I have never seen @@rowcount being set, only ever read. What I think is meant is set rowcount, but this cannot be set to a parameter, so you are back to dynamic sql

                SomeGuyThatIsMe wrote:

                I do not believe that Rowcount is available in sql server 2k5,

                Both rowcount (to set the number of rows returned) and @@rowcount (the number of rows affected) are both still alive and kicking in SQL Server 2005

                Bob Ashfield Consultants Ltd

                S Offline
                S Offline
                SomeGuyThatIsMe
                wrote on last edited by
                #7

                It works fine i have it done in multiple places mostly in stored procedures, and in some other queries. SET ROWCOUNT it works well for randomizing the data you pull from a table with no PK. @@ROWCOUNT might be read only, ROWCOUNT is the correct one to use.

                Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                A A 2 Replies Last reply
                0
                • S SomeGuyThatIsMe

                  It works fine i have it done in multiple places mostly in stored procedures, and in some other queries. SET ROWCOUNT it works well for randomizing the data you pull from a table with no PK. @@ROWCOUNT might be read only, ROWCOUNT is the correct one to use.

                  Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                  A Offline
                  A Offline
                  Ashfield
                  wrote on last edited by
                  #8

                  Sorry, you can set rowcount to a parameter, I just couldn't type when I tried it :-O

                  Bob Ashfield Consultants Ltd

                  1 Reply Last reply
                  0
                  • S SomeGuyThatIsMe

                    You can use SET @@rowcount or create procedure ss(@s int) as EXEC 'select top ' + CAST(@s AS CHAR) + ' sno from summa1 order by newid()'

                    Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    SomeGuyThatIsMe wrote:

                    You can use SET @@rowcount or

                    I'm pretty sure you're thinking of SET ROWCOUNT and not SET @@ROWCOUNT. @@ROWCOUNT tells you how many rows were affected by a statement.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    1 Reply Last reply
                    0
                    • S SomeGuyThatIsMe

                      It works fine i have it done in multiple places mostly in stored procedures, and in some other queries. SET ROWCOUNT it works well for randomizing the data you pull from a table with no PK. @@ROWCOUNT might be read only, ROWCOUNT is the correct one to use.

                      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                      A Offline
                      A Offline
                      Alsvha
                      wrote on last edited by
                      #10

                      Although be aware that as far as I know, ROWCOUNT is being deprecated in 2K5 and it is removed from SQL Server 2K8 (or perhaps the one after 2K8)

                      --------------------------- Blogging about SQL, Technology and many other things

                      S 1 Reply Last reply
                      0
                      • A Alsvha

                        Although be aware that as far as I know, ROWCOUNT is being deprecated in 2K5 and it is removed from SQL Server 2K8 (or perhaps the one after 2K8)

                        --------------------------- Blogging about SQL, Technology and many other things

                        S Offline
                        S Offline
                        SomeGuyThatIsMe
                        wrote on last edited by
                        #11

                        yep thats what i read...they are replacing it with something, but i dont think it works quite the same.

                        Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

                        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