以下代码导致无限循环或执行速度非常慢:
CREATE FUNCTION [dbo].[CleanUriPart]
(
-- Add the parameters for the function here
@DirtyUriPart nvarchar(200)
)
RETURNS nvarchar(200)
AS
BEGIN;
-- Declare the return variable here
DECLARE @Result nvarchar(200);
DECLARE @i int;
SET @i = 1;
WHILE 1 = 1
BEGIN;
SET @i = PATINDEX('%[^a-zA-Z0-9.~_-]%', @DirtyUriPart COLLATE Latin1_General_BIN);
IF @i > 0
SET @DirtyUriPart = STUFF(@DirtyUriPart, @i, 1, '-');
ELSE
BREAK;
END;
-- Add the T-SQL statements to compute the return value here
SELECT @Result = @DirtyUriPart;
-- Return the result of the function
RETURN @Result;
END;
输入/输出应如下所示:
- 'abcdef' -> 'abcdef' 工作正常
- 'abc-def' -> 'abc-def' 导致无限循环
- 'abc*def' -> 'abc-def' 导致无限循环
- 等等
请帮忙!