sqlcurious
asked on
NEED TO BUILT A REPORT LIKE THIS
I HAVE ATTACHED TWO FILES ONE IS REPORT FORMAT AND ONE IS STORED PROC.
NEED HELP TO PASS MULTIPLE DIVSION NAMES IN THE @DIVISIONNAME PARAMETER
LIKE @DIVISIONNAME= 'dIVISION DESCRIPTION 2,DIVISION DESCRIPTION 3'
NOW IT ACCEPTS ONLY ONE LIKE THIS
@DIVISIONNAME= 'dIVISION DESCRIPTION 2'
ITS URGENT PLEASE
rpteBidProjectDetailCosting.pdf
StoredProc.txt
NEED HELP TO PASS MULTIPLE DIVSION NAMES IN THE @DIVISIONNAME PARAMETER
LIKE @DIVISIONNAME= 'dIVISION DESCRIPTION 2,DIVISION DESCRIPTION 3'
NOW IT ACCEPTS ONLY ONE LIKE THIS
@DIVISIONNAME= 'dIVISION DESCRIPTION 2'
ITS URGENT PLEASE
rpteBidProjectDetailCosting.pdf
StoredProc.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The following code assumes that the function called list_to_tbl as shown in the code snippet below exists.
(Please note: this function differs slightly from the one in the article because you're dealing with strings instead of numbers)
To filter on your divisions, instead of
you can do
Please note that the declaration of your @DivisionName parameter should take into account that multiple values are passed. Which means that the length of the parameter should be: maximum length of division name multiplied by maximum number of divisions that the report user will select.
One more note: I notice that you've also got DivisionIDs in your database. It would be better if you could set up your parameter to use that ID as value for the parameter. That way you get a list of comma-separated numbers (as explained in the article referenced earlier) instead of strings. Obviously that means that your stored proc code would also need to change a bit.
That's just a note though, your method will work too.
Hope this helps?
(Please note: this function differs slightly from the one in the article because you're dealing with strings instead of numbers)
To filter on your divisions, instead of
set @SQL= @Sql + ' And D.DivisionName LIKE ''%'+@DivisionName+'%'''
you can do
set @SQL= @Sql + ' And D.DivisionName IN (select * from list_to_tbl(@DivisionName))'
Please note that the declaration of your @DivisionName parameter should take into account that multiple values are passed. Which means that the length of the parameter should be: maximum length of division name multiplied by maximum number of divisions that the report user will select.
One more note: I notice that you've also got DivisionIDs in your database. It would be better if you could set up your parameter to use that ID as value for the parameter. That way you get a list of comma-separated numbers (as explained in the article referenced earlier) instead of strings. Obviously that means that your stored proc code would also need to change a bit.
That's just a note though, your method will work too.
Hope this helps?
CREATE FUNCTION list_to_tbl (@list nvarchar(MAX))
RETURNS @tbl TABLE (string varchar(250) NOT NULL) AS
BEGIN
DECLARE @pos int,
@nextpos int,
@valuelen int
SELECT @pos = 0, @nextpos = 1
WHILE @nextpos > 0
BEGIN
SELECT @nextpos = charindex(',', @list, @pos + 1)
SELECT @valuelen = CASE WHEN @nextpos > 0
THEN @nextpos
ELSE len(@list) + 1
END - @pos - 1
INSERT @tbl (string)
VALUES (substring(@list, @pos + 1, @valuelen))
SELECT @pos = @nextpos
END
RETURN
END
ASKER