T-SQL IN Operator not working

vj78 21 Reputation points
2021-07-29T13:14:38.14+00:00

SELECT * FROM Table a
WHERE column1 IN (SELECT Column2 FROM Table2 b)

This works if I use TOP 1
SELECT * FROM Table a
WHERE column1 IN (SELECT TOP 1 Column2 FROM Table2 b)

Developer technologies Transact-SQL
{count} votes

9 answers

Sort by: Most helpful
  1. vj78 21 Reputation points
    2021-07-29T13:42:43.23+00:00

    error message: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= pr when the subquery is used as an expression.


  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-07-29T14:56:53.29+00:00

    This works for me:

    CREATE TABLE #t1 (a int NOT NULL)
    INSERT #t1 (a) VALUES (1), (2), (3), (4)
    CREATE TABLE #t2 (n int NOT NULL)
    INSERT #t2 (n) VALUES (1), (2), (13), (14)
    SELECT a.a FROM #t1 a WHERE a.a IN (SELECT b.n FROM #t2 b)
    go
    DROP TABLE #t1, #t2
    

    I guess your actual query looks different, but it is difficult to troubleshoot a query we don't see.

    0 comments No comments

  3. vj78 21 Reputation points
    2021-07-29T15:31:43.21+00:00

    DECLARE @alenzi nvarchar(max) = N'

    (

    SELECT * FROM Table a
    WHERE column1 IN (SELECT Column2 FROM Table2 b)

    )

    EXEC (@alenzi )

    It is hitting a linked server on db2


  4. vj78 21 Reputation points
    2021-07-29T18:06:07.423+00:00

    DECLARE @itemnumbers table

    (

    ID varchar(1000)  
    

    )

    insert into @itemnumbers values ('1')

    insert into @itemnumbers values ('2')

    insert into @itemnumbers values ('3')

    insert into @itemnumbers values ('4')

    insert into @itemnumbers values ('5')

    DECLARE @alenzi nvarchar(MAX) = N'

       SELECT * FROM OPENQUERY  
    
       (  
    
              DB2System, N''  
    
              SELECT  *  
    
              FROM  
    
                     Table1 a  
    
                     WHERE column1 IN  ''''' + (SELECT ID FROM @itemnumbers ) + '''''  
    
                                     
    
                      
    
                     ''  
    
              )'  
    

    EXEC (@alenzi )

    0 comments No comments

  5. vj78 21 Reputation points
    2021-07-29T19:12:13.597+00:00

    DECLARE @itemnumbers table

    (

    ID varchar(1000)  
    

    )

    insert into @itemnumbers values ('1)

    insert into @itemnumbers values ('2')

    insert into @itemnumbers values ('3')

    DECLARE @alenzi nvarchar(MAX) = N'

       SELECT * FROM OPENQUERY  
    
       (  
    
              DB2System, N''  
    
              SELECT  *  
    
              FROM  
    
                     table1 a  
    
                     WHERE column1    IN  ''''' + (SELECT TOP 1 ID FROM @itemnumbers ) + '''''  
    
                                     
    
                      
    
                     ''  
    
              )'  
    

    EXEC (@alenzi )

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.