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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need help with creating dynamic SQL statement

Need help with creating dynamic SQL statement

Scheduled Pinned Locked Moved Database
databasehelpquestion
2 Posts 2 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.
  • J Offline
    J Offline
    James Shao
    wrote on last edited by
    #1

    Hi I am trying to create a stock scanner for my website. The user would choose and add certain criterias one by one, and a dynamic SQL statement would need to be created on the fly to retrieve the data. Currently I am able to do this by appending pieces of SQL string after a long SQL string like the following: Whenever "Add Criteria" button is pressed: newQuery = "AND criteria > x" userCriteria = userCriteria + newQuery When "Retrieve Information" button is pressed: Querystring = "Select * WHERE Date = date " & userCriteria Is there a more efficient mechanism? If not that's also fine as it currently works well, but I just need some reassurance from experts. :) Thanks!

    modified on Monday, December 21, 2009 12:07 AM

    M 1 Reply Last reply
    0
    • J James Shao

      Hi I am trying to create a stock scanner for my website. The user would choose and add certain criterias one by one, and a dynamic SQL statement would need to be created on the fly to retrieve the data. Currently I am able to do this by appending pieces of SQL string after a long SQL string like the following: Whenever "Add Criteria" button is pressed: newQuery = "AND criteria > x" userCriteria = userCriteria + newQuery When "Retrieve Information" button is pressed: Querystring = "Select * WHERE Date = date " & userCriteria Is there a more efficient mechanism? If not that's also fine as it currently works well, but I just need some reassurance from experts. :) Thanks!

      modified on Monday, December 21, 2009 12:07 AM

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      If it works then you can only improve the structure of the query otherwise it is an exercise in string concatenation. If the where clause is the only dynamic piece and the elements are known you can use something like

      Where (IsNull(@AttrID, -1) = -1 OR AttrID = @AttrID)

      where the default value is null or -1. Caveat, too many of these (about 6 IIRC) MAY affect the query performance I believe. I use it all the time and have had up to 8 elements in the where clause and it worked fine. The only benefit is it is not dynamic, not a huge issue!

      Never underestimate the power of human stupidity RAH

      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