I have in my application a user defined function which takes a comma separated list as an argument. It splits the items and plugs them in to a table variable and returns the result.
This function works well, except that when the items in the comma separated list exceed 1000, it ignores the remainder. That is to say, if I plug in 1239, the first 1000 rows will be returned and the remaining 239 are entirely ignored. There are no errors when this occurs.
I can't help but feel that this is due to some sort of limitation that I should know about, but I can't seem to find any information about it. Is it a limitation on the amount of rows that can be stored in a table variable? Or am I missing something in the actual code itself? Can anyone assist? Going squirrely-eyed over here.
ALTER FUNCTION [dbo].[ufnConvertArrayToIntTable] (@IntArray VARCHAR(8000))
RETURNS @retIntTable TABLE
(
ID int
)
AS
BEGIN
DECLARE @Delimiter char(1)
SET @Delimiter = ','
DECLARE @Item varchar(8)
IF CHARINDEX(@Delimiter,@IntArray,0) <> 0
BEGIN
WHILE CHARINDEX(@Delimiter,@IntArray,0) <> 0
BEGIN
SELECT
@Item = RTRIM(LTRIM(SUBSTRING(@IntArray,1,CHARINDEX(@Delimiter,@IntArray,0)-1))),
开发者_StackOverflow中文版 @IntArray = RTRIM(LTRIM(SUBSTRING(@IntArray,CHARINDEX(@Delimiter,@IntArray,0)+1,LEN(@IntArray))))
IF LEN(@Item) > 0
INSERT INTO @retIntTable SELECT @Item
END
IF LEN(@IntArray) > 0
INSERT INTO @retIntTable SELECT @IntArray
END
ELSE
BEGIN
IF LEN(@IntArray) > 0
INSERT INTO @retIntTable SELECT @IntArray
END
RETURN
END;
You define your input variable as varchar(8000) and your @Item variable is varchar(8). Are your items typically 8 characters each? Is the string you send in w/ over 1000 items more than 8000 characters? Try changing your input to varchar(max) instead.
Are all of your comma seperated values 8 chars long? If so, then the input parameter will only be able to hold 888 (8000 / 9(including the comma) of them..
It's because your input parameter is limited to 8000 characters.
You might try calling the function using substring... Maybe:
WHERE
[myField] IN(Select ID from [dbo].[ufnConvertArrayToIntTable](substring(@inputarray, 1, 4000))
OR
[myField] IN(Select ID from [dbo].[ufnConvertArrayToIntTable](substring(@inputarray, 4001, 8000))
...
精彩评论