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. Other Discussions
  3. The Weird and The Wonderful
  4. Excellent stored procedure

Excellent stored procedure

Scheduled Pinned Locked Moved The Weird and The Wonderful
databaseruby
6 Posts 4 Posters 3 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.
  • E Offline
    E Offline
    eggsovereasy
    wrote on last edited by
    #1

    I had to go back and look at how the product search was working on an old classic asp application. I happened to find this little gem lurking in the database. Fortunately there is some sanitizing going on in the asp to prevent a sql injection but a little piece of me died when I saw it. ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

    L X K 3 Replies Last reply
    0
    • E eggsovereasy

      I had to go back and look at how the product search was working on an old classic asp application. I happened to find this little gem lurking in the database. Fortunately there is some sanitizing going on in the asp to prevent a sql injection but a little piece of me died when I saw it. ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Agile development in it's purest form :p

      xacc.ide
      The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

      1 Reply Last reply
      0
      • E eggsovereasy

        I had to go back and look at how the product search was working on an old classic asp application. I happened to find this little gem lurking in the database. Fortunately there is some sanitizing going on in the asp to prevent a sql injection but a little piece of me died when I saw it. ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

        X Offline
        X Offline
        Xiangyang Liu
        wrote on last edited by
        #3

        eggsovereasy wrote:

        ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

        I wonder what will happen if I try the following:

        exec [dbo].[ily_products_search_keyword] ' delete from products select * from products'

        :)

        My .NET Business Application Framework My Home Page

        E 1 Reply Last reply
        0
        • X Xiangyang Liu

          eggsovereasy wrote:

          ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

          I wonder what will happen if I try the following:

          exec [dbo].[ily_products_search_keyword] ' delete from products select * from products'

          :)

          My .NET Business Application Framework My Home Page

          E Offline
          E Offline
          eggsovereasy
          wrote on last edited by
          #4

          You need a ";--" at the end because of the order by, but yeah, its pretty bad.

          X 1 Reply Last reply
          0
          • E eggsovereasy

            You need a ";--" at the end because of the order by, but yeah, its pretty bad.

            X Offline
            X Offline
            Xiangyang Liu
            wrote on last edited by
            #5

            eggsovereasy wrote:

            You need a ";--" at the end because of the order by

            Not really, the "order by" was taken care of by the preceding "select * ", although it is unnecessary since no row will be returned. :)

            My .NET Business Application Framework My Home Page

            1 Reply Last reply
            0
            • E eggsovereasy

              I had to go back and look at how the product search was working on an old classic asp application. I happened to find this little gem lurking in the database. Fortunately there is some sanitizing going on in the asp to prevent a sql injection but a little piece of me died when I saw it. ALTER procedure [dbo].[ily_products_search_keyword] (@sWhere varchar(2000) ) as set nocount on declare @sql varchar(8000) select @sql = 'SELECT products.product_id,products.product_name,products.file_url, ' select @sql = @sql + 'products.print_url, products.product_desc ' select @sql = @sql + ' FROM products ' select @sql = @sql + ' WHERE products.active = 1 ' select @sql = @sql + @sWhere select @sql = @sql + ' ORDER BY products.product_name ' exec(@sql)

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #6

              The code is optimized for "readability" and "maintability", so everybody understands what is going on. Maybe the compiler does the rest of the work via detecting the first four @sql string statements are constants, and make one string of them.:cool: The sql varchar(8000) seems to be a little to big. But better too big than to short. X| And a clever compiler knows that the constants strings and 2000 are less than 8000. I dont know how clever a compiler of "today" is. :confused:

              Greetings from Germany

              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