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. Sql query , comparing a table field to a list of values , condition (MS SQL)

Sql query , comparing a table field to a list of values , condition (MS SQL)

Scheduled Pinned Locked Moved Database
database
5 Posts 5 Posters 28 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.
  • Z Offline
    Z Offline
    zira1789
    wrote on last edited by
    #1

    hi ,
    i have a list of id values ( listOfProducts ) returned from a dropdown list , i need to use them to filter in a sql request

    something like this :

    SELECT stock_id , loc_id

    FROM S_ware D

    WHERE D.product_id IN (listOfProducts) (not sure about that)

    i need to get all the records that have a product id that is contained inside the list from the dropdown

    thanks

    L A R 3 Replies Last reply
    0
    • Z zira1789

      hi ,
      i have a list of id values ( listOfProducts ) returned from a dropdown list , i need to use them to filter in a sql request

      something like this :

      SELECT stock_id , loc_id

      FROM S_ware D

      WHERE D.product_id IN (listOfProducts) (not sure about that)

      i need to get all the records that have a product id that is contained inside the list from the dropdown

      thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See SQL Tryit Editor v1.6[^]

      1 Reply Last reply
      0
      • Z zira1789

        hi ,
        i have a list of id values ( listOfProducts ) returned from a dropdown list , i need to use them to filter in a sql request

        something like this :

        SELECT stock_id , loc_id

        FROM S_ware D

        WHERE D.product_id IN (listOfProducts) (not sure about that)

        i need to get all the records that have a product id that is contained inside the list from the dropdown

        thanks

        A Offline
        A Offline
        Anand RB 2021
        wrote on last edited by
        #3

        Send the list of products as comma seperated to parameter List of Products and change this query into Dynamic query.

        For ex: @listOfProducts is parameter. Send as tilde or comma seperated to SQL query

        Inside Query, split the comma seperated and change to list. Use the below script.

        DECLARE @listOfProducts VARCHAR(2000)
        SET @listOfProducts='174~175~78~77~191~399~451'
        IF OBJECT_ID('tempdb..#listOfProducts') IS NOT NULL DROP TABLE #listOfProducts

        SELECT
        DISTINCT Split.a.value('.', 'VARCHAR(100)') AS Prod INTO #listOfProducts
        FROM (SELECT
        CAST ('' + REPLACE(@listOfProducts, '~', '') + '' AS XML) AS String )
        AS A CROSS APPLY String.nodes ('/M') AS Split(a)
        WHERE Split.a.value('.', 'VARCHAR(100)')<>''

        SELECT stock_id , loc_id
        FROM S_ware D
        WHERE D.product_id IN (select Prod from #listOfProducts)

        CHill60C 1 Reply Last reply
        0
        • A Anand RB 2021

          Send the list of products as comma seperated to parameter List of Products and change this query into Dynamic query.

          For ex: @listOfProducts is parameter. Send as tilde or comma seperated to SQL query

          Inside Query, split the comma seperated and change to list. Use the below script.

          DECLARE @listOfProducts VARCHAR(2000)
          SET @listOfProducts='174~175~78~77~191~399~451'
          IF OBJECT_ID('tempdb..#listOfProducts') IS NOT NULL DROP TABLE #listOfProducts

          SELECT
          DISTINCT Split.a.value('.', 'VARCHAR(100)') AS Prod INTO #listOfProducts
          FROM (SELECT
          CAST ('' + REPLACE(@listOfProducts, '~', '') + '' AS XML) AS String )
          AS A CROSS APPLY String.nodes ('/M') AS Split(a)
          WHERE Split.a.value('.', 'VARCHAR(100)')<>''

          SELECT stock_id , loc_id
          FROM S_ware D
          WHERE D.product_id IN (select Prod from #listOfProducts)

          CHill60C Offline
          CHill60C Offline
          CHill60
          wrote on last edited by
          #4

          Just for the record, that is not a Dynamic Query

          1 Reply Last reply
          0
          • Z zira1789

            hi ,
            i have a list of id values ( listOfProducts ) returned from a dropdown list , i need to use them to filter in a sql request

            something like this :

            SELECT stock_id , loc_id

            FROM S_ware D

            WHERE D.product_id IN (listOfProducts) (not sure about that)

            i need to get all the records that have a product id that is contained inside the list from the dropdown

            thanks

            R Offline
            R Offline
            RedDk
            wrote on last edited by
            #5

            There's no table named "S_ware" ... so the FROM clause will issue (most likely) "Invalid object name". I've also noticed that, in the second post to this thread, you've given a bit more information ... but you're declaring a variable as VARCHAR; the SET is ok. But suddenly 'listOfProducts' has become a temporary TABLE. So there are naming issues also. This all sounds like pedantry from me so I'll quit. Suffice to say I'd look up TABLE, SET, DECLARE, and the advanced XML in the BOL for SQL Server version you think you have.

            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