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. stored procedures

stored procedures

Scheduled Pinned Locked Moved Database
databasedata-structuresquestion
14 Posts 5 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.
  • R rcwoods

    hi i want to be able to give the user the option to select either Top 10, Top 20 or Top 30 from a result set. In my page they select from a drop down list and it must produce the graph accordingly, I would like to make 'Top' figure to be a variable so i only have to do one stored procedure.Not one for each Top 10, Top 20 or Top 30. Is this possible? Or do i have to create a stored procedure for each and then just call the appropriate procedure in my code? Thanks

    G Offline
    G Offline
    Giorgi Dalakishvili
    wrote on last edited by
    #2

    You can have a stored procedure which builds dynamic sql according to user input and executes it. There can be an easier and/or more convenient way but I'm not aware of.

    #region signature my articles #endregion

    P 1 Reply Last reply
    0
    • G Giorgi Dalakishvili

      You can have a stored procedure which builds dynamic sql according to user input and executes it. There can be an easier and/or more convenient way but I'm not aware of.

      #region signature my articles #endregion

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #3

      Giorgi Dalakishvili wrote:

      There can be an easier and/or more convenient way but I'm not aware of.

      I don't think there is. He can just have a parameter into the stored proc to specify how many top values he wants to retrieve. That would be my approach.

      "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

      G 1 Reply Last reply
      0
      • P Paul Conrad

        Giorgi Dalakishvili wrote:

        There can be an easier and/or more convenient way but I'm not aware of.

        I don't think there is. He can just have a parameter into the stored proc to specify how many top values he wants to retrieve. That would be my approach.

        "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #4

        Paul Conrad wrote:

        He can just have a parameter into the stored proc to specify how many top values he wants to retrieve. That would be my approach.

        I also thought about that but top clause doesn't accepts variables, you can only specify integers

        #region signature my articles #endregion

        P 1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          Paul Conrad wrote:

          He can just have a parameter into the stored proc to specify how many top values he wants to retrieve. That would be my approach.

          I also thought about that but top clause doesn't accepts variables, you can only specify integers

          #region signature my articles #endregion

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #5

          I forgot about TOP not allowing any variables.

          "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          G 1 Reply Last reply
          0
          • P Paul Conrad

            I forgot about TOP not allowing any variables.

            "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #6

            Just found out that in sql server 2005 TOP supports variables so you can write a parametrized sproc.

            #region signature my articles #endregion

            P 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              Just found out that in sql server 2005 TOP supports variables so you can write a parametrized sproc.

              #region signature my articles #endregion

              P Offline
              P Offline
              Paul Conrad
              wrote on last edited by
              #7

              That is interesting, because I tried and it complained that it wasn't valid.

              "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

              G 1 Reply Last reply
              0
              • P Paul Conrad

                That is interesting, because I tried and it complained that it wasn't valid.

                "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #8

                Did you put braces around the variable?

                #region signature my articles #endregion

                P 1 Reply Last reply
                0
                • G Giorgi Dalakishvili

                  Did you put braces around the variable?

                  #region signature my articles #endregion

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #9

                  I just tried and it works! Wow, I learned something new today :laugh:

                  "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                  G 1 Reply Last reply
                  0
                  • P Paul Conrad

                    I just tried and it works! Wow, I learned something new today :laugh:

                    "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #10

                    Paul Conrad wrote:

                    Wow, I learned something new today

                    I also learned something new :)

                    #region signature my articles #endregion

                    P 1 Reply Last reply
                    0
                    • R rcwoods

                      hi i want to be able to give the user the option to select either Top 10, Top 20 or Top 30 from a result set. In my page they select from a drop down list and it must produce the graph accordingly, I would like to make 'Top' figure to be a variable so i only have to do one stored procedure.Not one for each Top 10, Top 20 or Top 30. Is this possible? Or do i have to create a stored procedure for each and then just call the appropriate procedure in my code? Thanks

                      A Offline
                      A Offline
                      amraouf
                      wrote on last edited by
                      #11

                      use this: create spTest ( @top int) as begin declare @text nvarchar(500) set @text = 'select top ' + @top + ' from table where 1=1' sp_executesql @text end

                      1 Reply Last reply
                      0
                      • G Giorgi Dalakishvili

                        Paul Conrad wrote:

                        Wow, I learned something new today

                        I also learned something new :)

                        #region signature my articles #endregion

                        P Offline
                        P Offline
                        Paul B
                        wrote on last edited by
                        #12

                        Okay, so as far as I understand it, having variable SQL queries in a stored procedure (except the obvious WHERE clause exception) is generally a good way to screw up the execution plan... So, seeing your responses to the question, why didn't you just do the following? I'm using the Northwind Database as the test DB.CREATE PROC TestIdead @number smallint AS if @number = 10 begin select top 10 * from Customers end else if @number = 20 begin select top 20 * from Customers end else if @number = 30 begin select top 30 * from Customers end
                        In hindsight, you could probably reduce the IF...ELSE stuff down to a CASE statement as well... Just wondering why you opt for dynamic SQL queries rather than a logic structure that would result in a faster executing stored procedure (because the queries are static and thus don't modify the execution plan)? I'm a bit new to SQL and stored procs, so please enlighten me. :)

                        G 1 Reply Last reply
                        0
                        • P Paul B

                          Okay, so as far as I understand it, having variable SQL queries in a stored procedure (except the obvious WHERE clause exception) is generally a good way to screw up the execution plan... So, seeing your responses to the question, why didn't you just do the following? I'm using the Northwind Database as the test DB.CREATE PROC TestIdead @number smallint AS if @number = 10 begin select top 10 * from Customers end else if @number = 20 begin select top 20 * from Customers end else if @number = 30 begin select top 30 * from Customers end
                          In hindsight, you could probably reduce the IF...ELSE stuff down to a CASE statement as well... Just wondering why you opt for dynamic SQL queries rather than a logic structure that would result in a faster executing stored procedure (because the queries are static and thus don't modify the execution plan)? I'm a bit new to SQL and stored procs, so please enlighten me. :)

                          G Offline
                          G Offline
                          Giorgi Dalakishvili
                          wrote on last edited by
                          #13

                          You don't need all those IF...Else stuff. Here is a sproc which works for all possible values CREATE PROC TestIdead @number smallint AS select top (@number) from Customers

                          #region signature my articles #endregion

                          P 1 Reply Last reply
                          0
                          • G Giorgi Dalakishvili

                            You don't need all those IF...Else stuff. Here is a sproc which works for all possible values CREATE PROC TestIdead @number smallint AS select top (@number) from Customers

                            #region signature my articles #endregion

                            P Offline
                            P Offline
                            Paul B
                            wrote on last edited by
                            #14

                            hehe... Unfortunately, I can't test that as I'm running against an SQL2000 server... and my SQL needs to be backwards compatible so... No variables in TOP statement for me! Also, you didn't answer the question: Will the stored procedure I wrote run as fast or faster than the solution which uses the dynamic SQL query?

                            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