2017-06-19 48 views
0

我想編寫一個存儲過程,以過濾其中一些可能爲空或NULL的某些列。下面的代碼是我的,但它不起作用。使用ltrim和itrim

procedure .[s p_organization](@expert nvarchar(100), 
    @name nvarchar(100), @last_name nvarchar(200), 
    @organization n varchar(200)) 




where 
    ((Person.expert LIKE @expert or Person.expert LIKE @expert) 
    and (is null(l trim(r trim @last_name)),'')='' 
    or person.last_name like @last_name + '%' 
    and Person.Name LIKE @name 
    or @name is null 
    and Organization.Name LIKE N'%'[email protected] +'%') 
+0

「SELECT」子句在哪? –

+0

這是無效的標準SQL。您正在使用哪些DBMS? –

+0

查詢中存在太多的語法和邏輯錯誤 –

回答

0

使用orand混合時添加()

  • A AND B OR C將返回C完全獨立的A AND B
  • A AND (B OR C) will return A和B or A和C`(取決於數據

在你的案件的內容:

((Person.expert LIKE @expert or Person.expert LIKE @expert) 
    and ((is null(l trim(r trim @last_name)),")=" 
    or person.last_name like @last_name + '%') 
    and (Person.Name LIKE @name 
    or @name is null) 
    and Organization.Name LIKE N'%'[email protected] +'%') 

可能會更好地工作(你需要調整它自己的目的和結果

+0

不,它不能解決我的問題。 – tanin

+0

我並不期望它能解決您的完整問題,因爲我不知道您希望您的「或」語句在何處執行。 ''或'語句在使用複雜語句時需要使用'()'作爲正確的行爲。 –