2013-07-03 147 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

謝謝你,雖然它看起來有點慢,但它工作得很好。是,還是隻有這臺電腦,我從弗雷德弗林斯通得到二手? –