I’m working in a client’s SQL Server 2005 database and I’m seeing some odd behavior. I have a little experience with 2008, but this is my first battle with 2005, so I’m hoping this turns out to be something simple related to my inexperience.
What am I trying to do? I am developing a sales dashboard in SSRS 2005. One of the report parameters is a multi-value parameter which I need to pass to a stored procedure. I found a work around in some blogs, as well as here on SO, and implemented a function which splits a comma delimited varchar, and returns a table. I need this function to return the correct values from within the procedure, since I am calling stored procedures for the SSRS datasets.
My Problem: When I execute the function directly from within SSMS for a given value, I receive a table with 7 rows (as expected). When I execute a procedure that calls that function, with the same values passed, it only returns 1 row in the table. The user executing the function and the procedure is also the owner of both objects.
I’ve done a lot of homework to get to this point prior to posting the question, so I hope I’ve done my due diligence here. The client hasn’t given me privileges to work with SQL Profiler, so I haven’t been able to dig in that direction.
I thought perhaps this could be a permissions issue, but the fact that the function is still executed and does return 1 row instead of 7 confused me. It appears to only return the first number from the comma dilimitted string.
My Question: What the heck is causing the behavior outlined below? Please let me know if I should provide any additional information.
Executed from SSMS:
declare
@SiteID varchar(max);
set @SiteID = '1,2,3,4,5,6,7';
BEGIN
exec usp_function_test @SiteID;
select * from udf_rpt_multivalueparamtotable(@SiteID,',',1)
END
Output from Procedure:
Val
---
1
Output from select statement:
Val
---
1
2
3
4
5
6
7
Function Code:
CREATE FUNCTION [dbo].[udf_RPT_MultiValueParamToTAble](
@String VARCHAR(max), /* input string */
@Delimeter char(1), /* delimiter */
@TrimSpace bit ) /* kill whitespace? */
RETURNS @Table TABLE ( [Val] VARCHAR(4000) )
AS
BEGIN
DECLARE @Val VARCHAR(4000)
WHILE LEN(@String) > 0
BEGIN
SET @Val = LEFT(@String,
ISNULL(NULLIF(CHARINDEX(@Delimeter, @String) - 1, -1),
LEN(@String)))
SET @String = SUBSTRING(@String,
ISNULL(NULLIF(CHARINDEX(@Delimeter, @String), 0),
LEN(@String)) + 1, LEN(@String))
IF @TrimSpace = 1 Set @Val = LTRIM(RTRIM(@Val))
INSERT INTO @Table ( [Val] )
VALUES ( @Val )
END
RETURN
END
Test Procedure Code:
CREATE PROCEDURE usp_function_test (@SiteID varchar)
AS
SET NOCOUNT ON
BEGIN
select * from udf_rpt_multivalueparamtotable(@SiteID,',',1)
END
Try defining a scale for the SP parameter –
When you declare a
varcharparameter without a scale, it defaults to a scale of 1 – your input value was being silently truncated.Reference here – http://msdn.microsoft.com/en-us/library/ms176089.aspx