1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
ALTER PROCEDURE dbo.Split
@text AS NVARCHAR(4000),
@marqueur AS NVARCHAR(4000)
AS
DECLARE @indexOf AS INT
DECLARE @result AS NVARCHAR(4000)
DECLARE @counter AS INT
SET @counter = 1
SET @result = ''
WHILE LEN(@text) > 0
BEGIN
IF LEN(@result) > 0
BEGIN
SET @result = @result + ', '
END
SET @indexOf = PATINDEX('%' + @marqueur + '%', @text)
IF @indexOf > 0
BEGIN
SET @result = @result + '''' + LTRIM(RTRIM(SUBSTRING(@text, 1, @indexOf-1))) + ''' AS c' + CAST(@counter AS NVARCHAR(256))
SET @text = SUBSTRING(@text, @indexOf+1, LEN(@text)-@indexOf)
END
ELSE
BEGIN
SET @result = @result + '''' + LTRIM(RTRIM(@text)) + ''' AS c' + CAST(@counter AS NVARCHAR(256))
BREAK
END
SET @counter = @counter + 1
END
EXEC('SELECT ' + @result)
GO |
Partager