2016-09-21 45 views
0

我已经创建了下面的存储过程:如何在存储过程中将多个字符作为变量传递?

ALTER PROCEDURE [dbo].[CountInJunction] 
     @Mod as nvarchar(10), 
     @Junction as nvarchar(10), 
     @PJ as nvarchar(10), 
     **@case as varchar(10)**, 
     @Date as varchar(20) 
    as 

begin 

declare @result as int 

select @result = count(distinct CONCAT ([UCID],[CALLSEGMENT])) 

from IVR_LINES 
where MODULE = @Mod and DATE = @date 
and EVENT_NAME = @Junction and **EVENT_VALUE in (@case)** 

insert into [dbo].[MainJuncTable] values(@Mod,@PJ,@Junction,@case,@result,null,null,@date) 

return @result 

end 

我想通过( '0', '5'),为@case

由于某种原因,我得到0作为结果,这是不正确的。它似乎是SP不能正确解释('0','5')。 我一直试图多个组合如:

'0', '5'

'0' + ' '+ 5''

'0,5'

等等。

没有用。

有没有什么方法可以正确传递这些字符?

谢谢。

+1

看到这个漂亮的[由厄兰Sommarskog文章(HTTP://www.sommarskog .SE /阵列合sql.html) – Andomar

回答

0

发送的值作为像(“0,5”)的单个字符串

然后在条件U需要分割,并选择象的值,

where EVENT_VALUE in (select val from Split(@case,',')) 

分裂是用户定义的功能,您需要先使用它才能创建。

CREATE FUNCTION [dbo].[Split] 
(
    @delimited nvarchar(max), 
    @delimiter nvarchar(100) 
) RETURNS @t TABLE 
(
-- Id column can be commented out, not required for sql splitting string 
    id int identity(1,1), -- I use this column for numbering splitted parts 
    val nvarchar(max) 
) 
AS 
BEGIN 
    declare @xml xml 
    set @xml = N'<root><r>' + replace(@delimited,@delimiter,'</r><r>') + '</r></root>' 

    insert into @t(val) 
    select 
    r.value('.','varchar(max)') as item 
    from @xml.nodes('//root/r') as records(r) 

    RETURN 
END 

GO 
0

在任何情况下,用这个作为你的参数值:“0,5”

但如何使用它依赖于SQL Server的版本,您正在使用。

如果你有2016年,那就是STRING_SPLIT。 https://msdn.microsoft.com/en-us/library/mt684588.aspx

如果你没有它,你可以创建一个函数。请参阅相关的计算器帖子:How to split a comma-separated value to columns 或者,如果你想行:SQL query to split column data into rows

(见更高的额定建议在这两个的)

相关问题