2013-07-03 242 views
0

我了解到,我可以使用类似搜索多个领域:TSQL搜索多个条件

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

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

但是,有没有方法来搜索比多手术室等多重标准?

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 

谢谢!

+0

这不是TSQL –

+3

@ t-clausen.dk - 'CONCAT'在SQL Server引入2012 –

+2

FTS是一种选择http://msdn.microsoft.com/en-us/library/ms142571。 aspx –

回答

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 
) 
+0

谢谢你,虽然它看起来有点慢,但它工作得很好。是,还是只有这台电脑,我从弗雷德弗林斯通得到二手? –