【发布时间】:2015-07-27 08:36:45
【问题描述】:
我有一个名为 GetReport 的存储过程
customers.branches_ID is int type
然而@Branches 参数是 varchar
@Branches 是“10,13534,554,776,767”
我想在 branch_ID 中搜索 10,13534,554,776,767,但是如果我转换/转换为 varchar,它对我不起作用。
ALTER PROCEDURE [dbo].[GetReport]
@fromDate datetime,
@toDate datetime,
@Branches varchar (500) = null
AS
BEGIN
SET NOCOUNT ON;
select * from customers where
(customers.CreatedDate between @fromDate and @toDate) and
(@Branches is null or CONVERT(varchar(500), customers.branches_ID) in(@Branches )) -- This part is not working for me
END
我该如何解决这个问题如何在 int 列内搜索多个逗号 varcvhar ?
谢谢。
【问题讨论】:
-
如果可能,更改设计以使用数据类型设计 来包含多个数据项,例如 xml 或表值参数。如果您无法更改设计,快速搜索应该会找到大量
sql split commas的结果。 T-SQL,与 大多数 语言一样,不会尝试检查您提供的单个字符串参数的内容,并且在发现该字符串中的逗号时,突然决定将其视为多个参数。 -
感谢您的帮助,请您分享简单的示例,如果可行,我将接受最佳答案
-
只搜索表值参数。查看 MSDN 上的this tutorial
-
@GarethD 如果 OP 传递了正确的类型,则无需拆分任何内容,即具有要搜索的值的 TVP
标签: sql-server stored-procedures