Folgende Funktion soll einen String anhand eines spearators zerlegen und die ergebnisse zurückgeben(als tabelle) schreiben. Wieso werden aber die ergebnisse sortiert?? Es ist doch ein simples insert!!
CREATE FUNCTION FHELP_SplitString(@String VARCHAR(8000), @Separator VARCHAR(8000))
RETURNS @OutPut TABLE(Value VARCHAR(8000))
AS
BEGIN
DECLARE @SeparatorLength INT,
@StartLocation INT,
@EndLocation INT
SELECT @SeparatorLength = LEN(@Separator), @StartLocation = 0,
@EndLocation = CHARINDEX(@Separator, @String)
IF @String = '' SET @String = NULL
IF ISNULL(@EndLocation, 0) = 0
SELECT @EndLocation = LEN(@String) + 1
WHILE ISNULL(@EndLocation, 0) > @StartLocation BEGIN
INSERT INTO @OutPut(Value) VALUES (RTRIM(LTRIM(SUBSTRING(@String, @StartLocation, @EndLocation-@StartLocation))))
SELECT @StartLocation = @EndLocation + @SeparatorLength,
@EndLocation = CHARINDEX(@Separator, @String, @StartLocation)
IF ISNULL(@EndLocation, 0) = 0
SELECT @EndLocation = LEN(@String) + 1
END
RETURN
END
CREATE FUNCTION FHELP_SplitString(@String VARCHAR(8000), @Separator VARCHAR(8000))
RETURNS @OutPut TABLE(Value VARCHAR(8000))
AS
BEGIN
DECLARE @SeparatorLength INT,
@StartLocation INT,
@EndLocation INT
SELECT @SeparatorLength = LEN(@Separator), @StartLocation = 0,
@EndLocation = CHARINDEX(@Separator, @String)
IF @String = '' SET @String = NULL
IF ISNULL(@EndLocation, 0) = 0
SELECT @EndLocation = LEN(@String) + 1
WHILE ISNULL(@EndLocation, 0) > @StartLocation BEGIN
INSERT INTO @OutPut(Value) VALUES (RTRIM(LTRIM(SUBSTRING(@String, @StartLocation, @EndLocation-@StartLocation))))
SELECT @StartLocation = @EndLocation + @SeparatorLength,
@EndLocation = CHARINDEX(@Separator, @String, @StartLocation)
IF ISNULL(@EndLocation, 0) = 0
SELECT @EndLocation = LEN(@String) + 1
END
RETURN
END
Comment