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. using comma separated list in where clause

using comma separated list in where clause

Scheduled Pinned Locked Moved Database
helpdatabasesql-serversysadminannouncement
5 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.
  • U Offline
    U Offline
    Uma Kameswari
    wrote on last edited by
    #1

    Hi, I am having a problem with the below statements when I give as below I am not getting any records.

    declare @list char(100)
    set @list = '''wc1'',''wc2'''
    select * from iv30300 where trxloctn in (@list)

    When I directly give the list I am getting the result.

    declare @list char(100)
    set @list = '''wc1'',''wc2'''
    select * from iv30300 where trxloctn in ('wc1','wc2')

    I am working on sql server 2005 version. I am not able to find out the error.

    W P P 3 Replies Last reply
    0
    • U Uma Kameswari

      Hi, I am having a problem with the below statements when I give as below I am not getting any records.

      declare @list char(100)
      set @list = '''wc1'',''wc2'''
      select * from iv30300 where trxloctn in (@list)

      When I directly give the list I am getting the result.

      declare @list char(100)
      set @list = '''wc1'',''wc2'''
      select * from iv30300 where trxloctn in ('wc1','wc2')

      I am working on sql server 2005 version. I am not able to find out the error.

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      AFAIK you cannot use variable to define an in-list with comma separated values. The contents of the variable is interpreted as a single value, not a value list. Also you wouldn't need the quotation marks inside the string. For example the following would work:

      declare @value1 varchar(100)
      declare @value2 varchar(100)
      set @value1 = 'wc1'
      set @value2 = 'wc2'
      select * from iv30300 where trxloctn in (@value1, @value2)

      Perhaps you could modify the in-list to a subquery.

      The need to optimize rises from a bad design.My articles[^]

      1 Reply Last reply
      0
      • U Uma Kameswari

        Hi, I am having a problem with the below statements when I give as below I am not getting any records.

        declare @list char(100)
        set @list = '''wc1'',''wc2'''
        select * from iv30300 where trxloctn in (@list)

        When I directly give the list I am getting the result.

        declare @list char(100)
        set @list = '''wc1'',''wc2'''
        select * from iv30300 where trxloctn in ('wc1','wc2')

        I am working on sql server 2005 version. I am not able to find out the error.

        P Offline
        P Offline
        Paddy Boyd
        wrote on last edited by
        #3

        There's an not error, you just can't do it the first way, the in clause doesn't take a variable like that.

        1 Reply Last reply
        0
        • U Uma Kameswari

          Hi, I am having a problem with the below statements when I give as below I am not getting any records.

          declare @list char(100)
          set @list = '''wc1'',''wc2'''
          select * from iv30300 where trxloctn in (@list)

          When I directly give the list I am getting the result.

          declare @list char(100)
          set @list = '''wc1'',''wc2'''
          select * from iv30300 where trxloctn in ('wc1','wc2')

          I am working on sql server 2005 version. I am not able to find out the error.

          P Offline
          P Offline
          pmpdesign
          wrote on last edited by
          #4

          Use XML

          DECLARE @CSV XML
          SET @CSV = '<key><id>1</id><id>2</id><id>3</id></key>'

          SELECT
          ParamValues.id.value('.','VARCHAR(100)')
          FROM
          @CSV.nodes('/key/id') AS ParamValues(id)

          Run that and you should get three rows with values 1,2 & 3. Join that to your query or use in the WHERE clause Don't forget to parse the XML before presenting it to SQL in case there are any nasties on the input

          B 1 Reply Last reply
          0
          • P pmpdesign

            Use XML

            DECLARE @CSV XML
            SET @CSV = '<key><id>1</id><id>2</id><id>3</id></key>'

            SELECT
            ParamValues.id.value('.','VARCHAR(100)')
            FROM
            @CSV.nodes('/key/id') AS ParamValues(id)

            Run that and you should get three rows with values 1,2 & 3. Join that to your query or use in the WHERE clause Don't forget to parse the XML before presenting it to SQL in case there are any nasties on the input

            B Offline
            B Offline
            Ben Fair
            wrote on last edited by
            #5

            Put the values into a table variable and join to it; in our projects we have a udf that parses a comma-separated string and returns a table variable of the data, it's not too hard to make one. Do that and then join to the table variable.

            Keep It Simple Stupid! (KISS)

            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