如何将multiply values参数传递给过程

时间:2017-07-11 07:55:24

标签: sql sql-server stored-procedures reporting-services ssrs-2016

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

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

并尝试了STRING_SPLIT功能。 但是,当我运行我的报告时,它会返回错误 将nvarchar值'1,2,3,4,5,6,7,8,9,10,11,12'转换为数据类型int时转换失败。 enter image description here

1 个答案:

答案 0 :(得分:0)

您无法为逗号分隔值提供varchar字符串,并希望查询将其作为' in'进行处理。名单。 可行的方法是在存储过程中连接sql语句,然后使用sp_executesql执行sql字符串。 使用string_split是这样做的一种方式,但是你必须要知道它为你提供了一个包含varchars而不是整数的表。 为了完整起见,我应该提一下最强大的'这样做的方法是将表类型变量传递给存储过程,如下所述: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