Question : PASS MULTI-VALUE TO A QUERY OR STOREPROCEDURE?

The query below run well when selecting single value; however, when being used in SSRS to query multi-values (@PPFNCL) I get the following error:

"An error occured during local report processing,query execution failed for data set "ReportDB'.
Line 15: incorrect syntax near ','

Should this be a stored procedure?  if yes, could somebody provide me the correct syntax code.

Thank you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
SELECT DISTINCT 
                      TOP 100 PERCENT VW_HPPATMFL.ccGrpName, VW_HPPATMFL.PPPATno, VW_HPPATMFL.PPBLDT, VW_HPPATMFL.PPADDT, 
                      VW_HPPATMFL.PPDSDT, HAABSTFL.AALOSD, VW_HPPATMFL.PPNAME, HPMEDRFL.PMPSSN, VW_HPPATMFL.PPTYPE, VW_HPPATMFL.PPMSRV, 
                      VW_HPPATMFL.PPFNCL + ' ' + HBFNCLFL.BNNAME AS FC_Desc, HPINPLFL.PLINCO, HPINCOFL.PINAME, HPINPLFL.PLPLCY, 
                      VW_HPPATMFL.PPPDOB, VW_HPPATMFL.PPCHGS, VW_HPPATMFL.PPADJU, VW_HPPATMFL.PPPYMT, VW_HPPATMFL.PPCBAL, 
                      HPINPLFL.PLPLN#
FROM         HAABSTFL LEFT OUTER JOIN
                      HBFNCLFL RIGHT OUTER JOIN
                      VW_HPPATMFL ON HBFNCLFL.BNGRP# = VW_HPPATMFL.PPGRPno AND HBFNCLFL.BNFNCL = VW_HPPATMFL.PPFNCL LEFT OUTER JOIN
                      HPINPLFL INNER JOIN
                      HPINCOFL ON HPINPLFL.PLINCO = HPINCOFL.PICMP# ON VW_HPPATMFL.PPGRPno = HPINPLFL.PLGRP# AND 
                      VW_HPPATMFL.PPPATno = HPINPLFL.PLPAT# AND VW_HPPATMFL.PPGRPno = HPINCOFL.PIGRP# LEFT OUTER JOIN
                      HPMEDRFL ON VW_HPPATMFL.PPGRPno = HPMEDRFL.PMGRP# AND VW_HPPATMFL.PPPATno = HPMEDRFL.PMPAT# ON 
                      HAABSTFL.AAGRP# = VW_HPPATMFL.PPGRPno AND HAABSTFL.AAPAT# = VW_HPPATMFL.PPPATno
WHERE     (VW_HPPATMFL.PPFNCL = @PPFNCL) AND (VW_HPPATMFL.PPDSDT BETWEEN @StartDate AND @EndDate) AND (HPINPLFL.PLPLN# = 1) AND 
                      (VW_HPPATMFL.ccGrpName = @GRPNAME)

Answer : PASS MULTI-VALUE TO A QUERY OR STOREPROCEDURE?

ok create a function like split

then your query becomes this


SELECT DISTINCT
                      TOP 100 PERCENT VW_HPPATMFL.ccGrpName, VW_HPPATMFL.PPPATno, VW_HPPATMFL.PPBLDT, VW_HPPATMFL.PPADDT,
                      VW_HPPATMFL.PPDSDT, HAABSTFL.AALOSD, VW_HPPATMFL.PPNAME, HPMEDRFL.PMPSSN, VW_HPPATMFL.PPTYPE, VW_HPPATMFL.PPMSRV,
                      VW_HPPATMFL.PPFNCL + ' ' + HBFNCLFL.BNNAME AS FC_Desc, HPINPLFL.PLINCO, HPINCOFL.PINAME, HPINPLFL.PLPLCY,
                      VW_HPPATMFL.PPPDOB, VW_HPPATMFL.PPCHGS, VW_HPPATMFL.PPADJU, VW_HPPATMFL.PPPYMT, VW_HPPATMFL.PPCBAL,
                      HPINPLFL.PLPLN#
FROM         HAABSTFL LEFT OUTER JOIN
                      HBFNCLFL RIGHT OUTER JOIN
                      VW_HPPATMFL ON HBFNCLFL.BNGRP# = VW_HPPATMFL.PPGRPno AND HBFNCLFL.BNFNCL = VW_HPPATMFL.PPFNCL LEFT OUTER JOIN
                      HPINPLFL INNER JOIN
                      HPINCOFL ON HPINPLFL.PLINCO = HPINCOFL.PICMP# ON VW_HPPATMFL.PPGRPno = HPINPLFL.PLGRP# AND
                      VW_HPPATMFL.PPPATno = HPINPLFL.PLPAT# AND VW_HPPATMFL.PPGRPno = HPINCOFL.PIGRP# LEFT OUTER JOIN
                      HPMEDRFL ON VW_HPPATMFL.PPGRPno = HPMEDRFL.PMGRP# AND VW_HPPATMFL.PPPATno = HPMEDRFL.PMPAT# ON
                      HAABSTFL.AAGRP# = VW_HPPATMFL.PPGRPno AND HAABSTFL.AAPAT# = VW_HPPATMFL.PPPATno
 Inner Join dbo.split(@PPFBCL,',') as X
 on  VW_HPPATMFL.PPFNCL = X.data
WHERE     VW_HPPATMFL.PPDSDT BETWEEN @StartDate AND @EndDate
 AND HPINPLFL.PLPLN# = 1
 AND VW_HPPATMFL.ccGrpName = @GRPNAME
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
CREATE FUNCTION dbo.Split
(
	@RowData nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1

	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))

	Return
END
Random Solutions  
 
programming4us programming4us