TSQL搜索多个条件

时间:2013-07-03 14:47:16

标签: sql-server tsql sql-server-2012

我了解到我可以使用以下内容搜索多个字段:

DECLARE @srch nvarchar(40) 
    SET @srch = '%something%'

SELECT * FROM DataTable dt
    WHERE CONCAT(dt.field1, dt.field2) LIKE @srch

但是有没有办法搜索除多个OR之外的多个条件?

DECLARE @srch1 nvarchar(40), @srch2 nvarchar(40), @srch3 nvarchar(40),  
    SET @srch1 = '%this%'
    SET @srch2 = '%that%'
    SET @srch3 = '%the other%'

SELECT * FROM DataTable dt
    WHERE  CONCAT(dt.field1, dt.field2) LIKE @srch1 
        OR CONCAT(dt.field1, dt.field2) LIKE @srch2 
        OR CONCAT(dt.field1, dt.field2) LIKE @srch3

谢谢!

1 个答案:

答案 0 :(得分:1)

这个怎么样?

DECLARE @srch TABLE (srch_field nvarchar(40))

INSERT INTO @srch VALUES ( '%this%'), ('%that%') ,('%the other%')

SELECT * FROM DataTable dt
WHERE  EXISTS (
  SELECT NULL FROM @srch s WHERE CONCAT(dt.field1, dt.field2) LIKE srch_field
)