我在下面有一個Stored Proc查詢,它涉及返回部分分隔的搜索字符串。例如,傳遞'wis,k'搜索字符串的搜索將返回所有帶有'wis'和'k'的ID的結果。我正在使用一個函數和一個交叉連接,但如果連接交叉連接會導致加載此SPROC時無法加載所有我需要的數據。我在考慮是否有條件的交叉連接,這樣當我的搜索字符串變量'@ReceiptNo'爲空時,我將省略交叉連接並允許顯示所有的數據。請提供建議。謝謝。條件下交叉連接
我的存儲過程的一部分:
FROM [Transact] T
LEFT JOIN [Outlet] O On (T.Outlet_Code = O.Code)
LEFT JOIN [SystemCode] SC on (CONVERT(NVARCHAR,T.Mode) = SC.Code)
CROSS JOIN DBO.SPLIT(@ReceiptNo , ',') --SPLIT function to seperate delimited string
Where
(
CardNo In
(
Select [CardNo]
FROM [Card]
WHERE [CardNo] = @CardNo
AND [DeletedBy] IS NULL
AND [DeletedOn] IS NULL
AND [MemberID] = @MemberId
)
)
and
(
(T.TransactDate Between @TransactDateFrom And @TransactDateTo
or @TransactDateFrom is null
or @TransactDateTo is null
)
and (T.TransactDate >= @TransactDateFrom
or @TransactDateFrom is null)
and (T.TransactDate <= @TransactDateTo
or @TransactDateTo is null)
and
(
(',' + @Mode +',' LIKE '%,' + CONVERT(VARCHAR, T.Mode) + ',%')
or @Mode is null
)
and (T.ReceiptNo LIKE '%' + VAL + '%') --This is the 'LIKE' condition to return desired search string results
or (@TransactDateFrom is null
and @TransactDateTo is null
and @Mode is null
and @Outlet_Code is null
and @ReceiptNo is null
)
)
Group by T.AutoID, TransactDate,TransactTime, SC.Name, O.Name
, ReceiptNo, AmountSpent, TransactPoints, VoidOn
您正在使用哪種RDBMS? – 2011-11-29 02:08:14