Define variable to use with IN operator (T-SQL)

后端 未结 14 2119
心在旅途
心在旅途 2020-11-28 04:24

I have a Transact-SQL query that uses the IN operator. Something like this:

select * from myTable where myColumn in (1,2,3,4)

Is there a wa

14条回答
  •  青春惊慌失措
    2020-11-28 05:03

    slight improvement on @LukeH, there is no need to repeat the "INSERT INTO": and @realPT's answer - no need to have the SELECT:

    DECLARE @MyList TABLE (Value INT) 
    INSERT INTO @MyList VALUES (1),(2),(3),(4)
    
    SELECT * FROM MyTable
    WHERE MyColumn IN (SELECT Value FROM @MyList)
    

提交回复
热议问题