2016-09-21 77 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

(見更高的額定建議在這兩個的)

相關問題