如何将乘法值参数传递给过程

How to pass multiply values parameter to a procedure

我想将乘法值参数传递给我的过程并将其用作过滤器。我的参数称为@Month,它的数据类型在过程中为 NVARCHAR(MAX)。 我使用过滤器作为

WHERE (cal.CalendarYear = @Year) AND (cal.MonthId IN (@Month))

并且还尝试了 STRING_SPLIT 功能。 然而,当我 运行 我的报告时,它 return 一个错误 将 nvarchar 值 '1,2,3,4,5,6,7,8,9,10,11,12' 转换为数据类型 int 时转换失败。

您不能给出一个带有逗号分隔值的 varchar 字符串,并期望查询将其作为 'in' 列表处理。 可行的方法是在存储过程中连接 sql 语句,然后使用 sp_executesql 执行 sql 字符串。 使用 string_split 是一种方法,但您必须知道它给您的 table 是 varchars,而不是整数。 为了完整起见,我应该提到最 'robust' 的方法是将 table 类型的变量传递给您的存储过程,如下所述:How to pass table value parameters to stored procedure from .net code。 使用 split_string,您可以尝试这样的操作:

select
 -- your fields
from
    manytables mt
    join string_split(@Month,',') months on cast(months.value as int) = cal.monthId
where 
    cal.Calenderyear = @year