我在下面有一个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