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. How to use in operator

How to use in operator

Scheduled Pinned Locked Moved Database
databasetutorial
21 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 Uma Kameswari

    hello, Can I give a list of values in a valiable and use this variable in select query where clause with in operator for instance declare @list nvarchar(1000) set @list = select words from sample select * from emp where dept in(@list) regards, Uma

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #8

    You can put a select statement inside the IN expression

    SELECT *
    FROM Table1
    WHERE SomeColumn IN (SELECT Something FROM Table2)

    And no need for pesky dynamic SQL.


    Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

    U 1 Reply Last reply
    0
    • _ _AK_

      yes you can give... You can use it something like this: declare @tt varchar(100) declare @sql varchar(100) set @tt = '''0004'','+'''0005'','+'''0006''' set @sql='select * from mst_employees where emp_no in ('+@tt+')' exec (@sql)

      Best Regards, Apurva Kaushal

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #9

      He wanted to use the result of another SELECT statement inside the IN() expression which is a valid thing to do.


      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

      _ 1 Reply Last reply
      0
      • C Colin Angus Mackay

        He wanted to use the result of another SELECT statement inside the IN() expression which is a valid thing to do.


        Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

        _ Offline
        _ Offline
        _AK_
        wrote on last edited by
        #10

        Actually what she has given was some values in a variable thats why I had given that type of solution.

        Best Regards, Apurva Kaushal

        C 1 Reply Last reply
        0
        • U Uma Kameswari

          I have two tables. one contians a column called keywords which contains individual words. other contains a column called list which contains a list of words separated by comma.Now i want to write a query to retrieve details from first table by searching the second table.How to do this Regards, Uma

          _ Offline
          _ Offline
          _AK_
          wrote on last edited by
          #11

          for this no need to go for the dynamic query you can directly use the method suggested by Colin.

          Best Regards, Apurva Kaushal

          U 1 Reply Last reply
          0
          • _ _AK_

            Actually what she has given was some values in a variable thats why I had given that type of solution.

            Best Regards, Apurva Kaushal

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #12

            Apurva Kaushal wrote:

            Actually what she has given was some values in a variable thats why I had given that type of solution

            But if you read the pseudo sql that she provided you can see what she is trying to do:

            declare @list nvarchar(1000)
            set @list = select words from sample
            select * from emp where dept in(@list)

            The second line, being an apparent result from a SELECT statement gives the indication that it isn't necessarily a variable she wants, but the result of the SELECT statement.


            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

            _ 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Apurva Kaushal wrote:

              Actually what she has given was some values in a variable thats why I had given that type of solution

              But if you read the pseudo sql that she provided you can see what she is trying to do:

              declare @list nvarchar(1000)
              set @list = select words from sample
              select * from emp where dept in(@list)

              The second line, being an apparent result from a SELECT statement gives the indication that it isn't necessarily a variable she wants, but the result of the SELECT statement.


              Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

              _ Offline
              _ Offline
              _AK_
              wrote on last edited by
              #13

              yep and there exactly I got confused and gave that solution. :(

              Best Regards, Apurva Kaushal

              C 1 Reply Last reply
              0
              • _ _AK_

                yep and there exactly I got confused and gave that solution. :(

                Best Regards, Apurva Kaushal

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #14

                Apurva Kaushal wrote:

                yep and there exactly I got confused and gave that solution.

                Don't worry about it. It wasn't explained too well. You sometimes need to read something like that a couple of times to really understand what is wanted.


                Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                _ 1 Reply Last reply
                0
                • C Colin Angus Mackay

                  You can put a select statement inside the IN expression

                  SELECT *
                  FROM Table1
                  WHERE SomeColumn IN (SELECT Something FROM Table2)

                  And no need for pesky dynamic SQL.


                  Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                  U Offline
                  U Offline
                  Uma Kameswari
                  wrote on last edited by
                  #15

                  Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then it will not work.now the table data is like this table1 col1 hyderabad delhi mumbai table2 col1 hyderabad,chennai,bangalore bangalore,kolkata,delhi bangalore mumbai,hyderabad now if I give select * from table1 where col1 in (select col1 from table2) i will not get the output can u checkl it once Regards, Uma

                  M M 2 Replies Last reply
                  0
                  • U Uma Kameswari

                    Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then it will not work.now the table data is like this table1 col1 hyderabad delhi mumbai table2 col1 hyderabad,chennai,bangalore bangalore,kolkata,delhi bangalore mumbai,hyderabad now if I give select * from table1 where col1 in (select col1 from table2) i will not get the output can u checkl it once Regards, Uma

                    M Offline
                    M Offline
                    Mark J Miller
                    wrote on last edited by
                    #16

                    You can create a table-valued function which uses a loop to split the csv list in table2 and insert them individually into a table variable. CREATE FUNCTION [dbo].[function_string_to_table] ( @string VARCHAR(MAX), @delimiter CHAR(1) ) RETURNS @output TABLE( data VARCHAR(256) ) BEGIN DECLARE @start INT, @end INT SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) WHILE @start < LEN(@string) BEGIN IF @end = 0 SET @end = LEN(@string) + 1 INSERT INTO @output (data) VALUES (SUBSTRING(@string, @start, @end - @start)) SET @start = @end + 1 SET @end = CHARINDEX(@delimiter, @string, @start) END RETURN END Then you can do this: DECLARE @values VARCHAR(MAX) SELECT @values = col1 FROM table2 WHERE FOO = 'BAR' SELECT * FROM table1 WHERE col1 IN ( SELECT data FROM dbo.function_string_to_table(@values, ',') )

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Apurva Kaushal wrote:

                      yep and there exactly I got confused and gave that solution.

                      Don't worry about it. It wasn't explained too well. You sometimes need to read something like that a couple of times to really understand what is wanted.


                      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                      _ Offline
                      _ Offline
                      _AK_
                      wrote on last edited by
                      #17

                      Colin Angus Mackay wrote:

                      You sometimes need to read something like that a couple of times to really understand what is wanted.

                      Yep that is correct.. Thanks for the suggestion. :)

                      Best Regards, Apurva Kaushal

                      C 1 Reply Last reply
                      0
                      • _ _AK_

                        for this no need to go for the dynamic query you can directly use the method suggested by Colin.

                        Best Regards, Apurva Kaushal

                        U Offline
                        U Offline
                        Uma Kameswari
                        wrote on last edited by
                        #18

                        Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then it will not work.now the table data is like this.I hope this will make you clear with my requirement. table1 col1 hyderabad delhi mumbai table2 col1 hyderabad,chennai,bangalore bangalore,kolkata,delhi bangalore mumbai,hyderabad now if I give select * from table1 where col1 in (select col1 from table2) i will not get the output can u check it once Regards, Uma

                        C 1 Reply Last reply
                        0
                        • U Uma Kameswari

                          Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then it will not work.now the table data is like this.I hope this will make you clear with my requirement. table1 col1 hyderabad delhi mumbai table2 col1 hyderabad,chennai,bangalore bangalore,kolkata,delhi bangalore mumbai,hyderabad now if I give select * from table1 where col1 in (select col1 from table2) i will not get the output can u check it once Regards, Uma

                          C Offline
                          C Offline
                          Colin Angus Mackay
                          wrote on last edited by
                          #19

                          Uma Kameswari wrote:

                          Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then...

                          ...it breaks the first normal form of database normalisation. The easiest solution from this point is to go to the dynamic SQL and use cursors to navigate through the rows. However, that is a very slow solution. If you can, it would be better if you could find some way of normalising the data so that it can be used more efficiently.


                          Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                          1 Reply Last reply
                          0
                          • _ _AK_

                            Colin Angus Mackay wrote:

                            You sometimes need to read something like that a couple of times to really understand what is wanted.

                            Yep that is correct.. Thanks for the suggestion. :)

                            Best Regards, Apurva Kaushal

                            C Offline
                            C Offline
                            Colin Angus Mackay
                            wrote on last edited by
                            #20

                            Now it looks like we were both a little bit correct and a little bit wrong. :) C'est la vie!


                            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                            1 Reply Last reply
                            0
                            • U Uma Kameswari

                              Yes we can put a select statement inside the IN expression but if the column contains a list of values separated by comma(,) then it will not work.now the table data is like this table1 col1 hyderabad delhi mumbai table2 col1 hyderabad,chennai,bangalore bangalore,kolkata,delhi bangalore mumbai,hyderabad now if I give select * from table1 where col1 in (select col1 from table2) i will not get the output can u checkl it once Regards, Uma

                              M Offline
                              M Offline
                              M H 1 2 3
                              wrote on last edited by
                              #21

                              http://www.projectdmx.com/tsql/sqlarrays.aspx[^] You should be able to use CharIndex as the article in the link above suggests as one solution. select t1.* from table1 t1 join table2 t2 on charindex(',' + t1.col1 + ',' , ',' + t2.col1 + ',') > 0

                              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