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. Dynamic where Clause

Dynamic where Clause

Scheduled Pinned Locked Moved Database
helptutorialquestion
4 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.
  • D Offline
    D Offline
    dabuskol
    wrote on last edited by
    #1

    1. With the statement below I was able to retrieved records using the @jv parameter. declare @jv varchar(100) set @jv = 'abh3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO 2. What is the solution if you have two value in one parameter like the example below, Does anyone know the answer. Please HELP :confused: I'm using it in my stored procdure. declare @jv varchar(100) set @jv = 'abh3','hal3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO Dabuskol

    R P 2 Replies Last reply
    0
    • D dabuskol

      1. With the statement below I was able to retrieved records using the @jv parameter. declare @jv varchar(100) set @jv = 'abh3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO 2. What is the solution if you have two value in one parameter like the example below, Does anyone know the answer. Please HELP :confused: I'm using it in my stored procdure. declare @jv varchar(100) set @jv = 'abh3','hal3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO Dabuskol

      R Offline
      R Offline
      Rocky Moore
      wrote on last edited by
      #2

      dabuskol wrote: set @jv = 'abh3','hal3' Not quite sure what you are doing here. SQL Server does not support arrays so it does not seem possible you will have two values in one parameter. You can break them up manually and then build the query and execute it dynamically. Rocky Moore <><

      1 Reply Last reply
      0
      • D dabuskol

        1. With the statement below I was able to retrieved records using the @jv parameter. declare @jv varchar(100) set @jv = 'abh3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO 2. What is the solution if you have two value in one parameter like the example below, Does anyone know the answer. Please HELP :confused: I'm using it in my stored procdure. declare @jv varchar(100) set @jv = 'abh3','hal3' SELECT jv,day, interval/4, sm, aiu, sum(att), ans,usg, acblk FROM c204 WHERE [day] between '7-21-2003' and '7-22-2003' and jv = COALESCE(@jv,jv) GROUP BY jv,day,interval/4,sm,aiu,ans,usg,acblk ORDER BY jv,day,interval/4,sm,aiu,ans,usg,acblk GO Dabuskol

        P Offline
        P Offline
        Peet Schultz
        wrote on last edited by
        #3

        Hi There Looks like you will have to revert to Dynamic sql. Build up the statement as required and then excecute. This is normally a bit slower than straight sql but should be able to do the trick. Regards Peet Schultz USE [PUBS] DECLARE @LAST_NAME NVARCHAR(100) DECLARE @SQL NVARCHAR(1000) SET @LAST_NAME = '(''White'',''Green'')' SET @SQL = 'SELECT * FROM AUTHORS WHERE [AU_LNAME] IN' + @LAST_NAME EXEC (@SQL)

        D 1 Reply Last reply
        0
        • P Peet Schultz

          Hi There Looks like you will have to revert to Dynamic sql. Build up the statement as required and then excecute. This is normally a bit slower than straight sql but should be able to do the trick. Regards Peet Schultz USE [PUBS] DECLARE @LAST_NAME NVARCHAR(100) DECLARE @SQL NVARCHAR(1000) SET @LAST_NAME = '(''White'',''Green'')' SET @SQL = 'SELECT * FROM AUTHORS WHERE [AU_LNAME] IN' + @LAST_NAME EXEC (@SQL)

          D Offline
          D Offline
          dabuskol
          wrote on last edited by
          #4

          Thank you for your help. my stored procedure is working fine and so far the speed of retrieving doesn't affect much. Hope to hear from you guys in the future... Good Luck:laugh: dabuskol

          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