如何使用 LIKE 运算符而不是 IN 运算符来处理多个逗号包含存储过程中的字符串
How can I use LIKE operator instead of IN operator for multiple commas contains string in stored procedure
我只是在我通过 C# 中的 LINQ 执行此操作之前为搜索项创建了存储过程,例如
//orders
if (objParam.OrderNumber != null && objParam.OrderNumber.Count > 0)
{
foreach (var orderNumber in objParam.OrderNumber)
{
orderNumbersBuilder.Append(',' + orderNumber.ToString());
}
}
var orderNbrs = orderNumbersBuilder.ToString().Trim(',').Split(',');
//Searching
(objParam.OrderNumber.Count == 0 || orderNbrs.Any(a => i.tblOrderMaster.OrderNumber.Contains(a)))
现在我想用存储过程来做。我使用 IN 运算符 得到结果,但我想使用 LIKE 运算符 例如
SELECT * FROM tblOrderMaster WHERE TrxNumber LIKE '%' + (SELECT * FROM STRING_SPLIT('1330,1329',',')) + '%'
我有多个过滤器,所以我不想使用函数和子查询,例如
--Params
@Account NVARCHAR(MAX) = NULL,
@OrderNumber NVARCHAR(MAX) = NULL,
@Carrier NVARCHAR(MAX) = NULL,
@ItemCode NVARCHAR(MAX) = NULL,
@OrderType NVARCHAR(MAX) = NULL,
@PONumber NVARCHAR(MAX) = NULL,
@SONumber NVARCHAR(MAX) = NULL
--columns start
--columns end
--Where condtions
(@ACCOUNT IS NULL OR @Account = '' OR partners.PartnerCode IN (select * from string_split(@ACCOUNT,','))) -- multi select filters started
AND
(@OrderNumber IS NULL OR @OrderNumber = '' OR orderMaster.OrderNumber IN (select * from string_split(@OrderNumber,',')))
AND
(@Carrier IS NULL OR @Carrier = '' OR carrier.Description IN (select * from string_split(@Carrier,',')))
AND
(@ItemCode IS NULL OR @ItemCode = '' OR itemMaster.ItemCode IN (select * from string_split(@ItemCode,',')))
AND
(@OrderType IS NULL OR @OrderType = '' OR orderMaster.OrderType IN (select * from string_split(@OrderType,',')))
AND
(@PONumber IS NULL OR @PONumber = '' OR orderMaster.PONumber IN (select * from string_split(@PONumber,',')))
AND
(@SONumber IS NULL OR @SONumber = '' OR orderMaster.SONumber IN (select * from string_split(@SONumber,',')))
你需要使用子查询;您不想这样做的事实不会改变您当前的设计。将查询与您拥有的文字值一起使用,它看起来像这样:
SELECT *
FROM dbo.tblOrderMaster OM
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT('1330,1329', ',') SS
WHERE OM.TrxNumber LIKE '%' + SS.[Value] + '%')
如果你真的不想使用子查询,那么使用table类型参数然后你可以执行JOIN
:
SELECT OM.*
FROM dbo.tblOrderMaster OM
JOIN @YourTableVariable YTV ON OM.TrxNumber LIKE '%' + YTV.SearchValue + '%'
我只是在我通过 C# 中的 LINQ 执行此操作之前为搜索项创建了存储过程,例如
//orders
if (objParam.OrderNumber != null && objParam.OrderNumber.Count > 0)
{
foreach (var orderNumber in objParam.OrderNumber)
{
orderNumbersBuilder.Append(',' + orderNumber.ToString());
}
}
var orderNbrs = orderNumbersBuilder.ToString().Trim(',').Split(',');
//Searching
(objParam.OrderNumber.Count == 0 || orderNbrs.Any(a => i.tblOrderMaster.OrderNumber.Contains(a)))
现在我想用存储过程来做。我使用 IN 运算符 得到结果,但我想使用 LIKE 运算符 例如
SELECT * FROM tblOrderMaster WHERE TrxNumber LIKE '%' + (SELECT * FROM STRING_SPLIT('1330,1329',',')) + '%'
我有多个过滤器,所以我不想使用函数和子查询,例如
--Params
@Account NVARCHAR(MAX) = NULL,
@OrderNumber NVARCHAR(MAX) = NULL,
@Carrier NVARCHAR(MAX) = NULL,
@ItemCode NVARCHAR(MAX) = NULL,
@OrderType NVARCHAR(MAX) = NULL,
@PONumber NVARCHAR(MAX) = NULL,
@SONumber NVARCHAR(MAX) = NULL
--columns start
--columns end
--Where condtions
(@ACCOUNT IS NULL OR @Account = '' OR partners.PartnerCode IN (select * from string_split(@ACCOUNT,','))) -- multi select filters started
AND
(@OrderNumber IS NULL OR @OrderNumber = '' OR orderMaster.OrderNumber IN (select * from string_split(@OrderNumber,',')))
AND
(@Carrier IS NULL OR @Carrier = '' OR carrier.Description IN (select * from string_split(@Carrier,',')))
AND
(@ItemCode IS NULL OR @ItemCode = '' OR itemMaster.ItemCode IN (select * from string_split(@ItemCode,',')))
AND
(@OrderType IS NULL OR @OrderType = '' OR orderMaster.OrderType IN (select * from string_split(@OrderType,',')))
AND
(@PONumber IS NULL OR @PONumber = '' OR orderMaster.PONumber IN (select * from string_split(@PONumber,',')))
AND
(@SONumber IS NULL OR @SONumber = '' OR orderMaster.SONumber IN (select * from string_split(@SONumber,',')))
你需要使用子查询;您不想这样做的事实不会改变您当前的设计。将查询与您拥有的文字值一起使用,它看起来像这样:
SELECT *
FROM dbo.tblOrderMaster OM
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT('1330,1329', ',') SS
WHERE OM.TrxNumber LIKE '%' + SS.[Value] + '%')
如果你真的不想使用子查询,那么使用table类型参数然后你可以执行JOIN
:
SELECT OM.*
FROM dbo.tblOrderMaster OM
JOIN @YourTableVariable YTV ON OM.TrxNumber LIKE '%' + YTV.SearchValue + '%'