Stored procedure help - multiple values within a parameter

Former Member
Former Member $organization

Hi all, I'm having a play in SSMS to expand my stored procedure writing skills and I've hit an obstacle that I'm not sure how to overcome. I'm writing a simple report that I want to build up to something more complex:

select customer_no, sum(cont_amt) from t_contribution

where fund_no in (1,2,3,4,5) and appeal_no in (1,2,3,4,5)

group by customer_no

That works fine.

Next I want to set up fund_no and appeal_no as variable parameters for my report, however as far as I understand it I am not able to use IN to set the variables. The reports I've written so far have only needed one value in each parameter so I have been able to use =, but that's not going to work in this instance because I need to be able to filter on multiple values. I understand from Mr Google that it's not possible to use IN in this context and that there are workarounds to get the same result, but it's a little bit complicated for me to comprehend and apply back to my specific case here.

Is anybody able to please explain how I need to change this stored procedure to allow multiple values within the fund and appeal variables?

This is what I've got at the moment using '=' which will only work on a single fund and appeal:

ALTER        PROCEDURE [dbo].[LRP_PHILANTH TEST REPORT_DEMO](

@fund_str varchar(4000),

@appeal_str varchar(4000)

)

AS

Set NoCount On  -- added auto CWR 8/9/2001


select customer_no, sum(cont_amt) from t_contribution

where fund_no = @fund_str and appeal_no = @appeal_str

group by customer_no

;

Many thanks for reading.

Parents
  • Try using the ft_split_list function 

    like this

    select customer_no, sum(cont_amt) from t_contribution
    where fund_no in (select Convert(int,element) from dbo.ft_split_list(@fund_str,','))
    and appeal_no  in (select Convert(int,element) from dbo.ft_split_list( @appeal_str,','))
    group by customer_no

     

     

Reply
  • Try using the ft_split_list function 

    like this

    select customer_no, sum(cont_amt) from t_contribution
    where fund_no in (select Convert(int,element) from dbo.ft_split_list(@fund_str,','))
    and appeal_no  in (select Convert(int,element) from dbo.ft_split_list( @appeal_str,','))
    group by customer_no

     

     

Children
No Data