ALTER PROCEDURE [dbo].[ObtenerListaCorreos] @OpcionId AS varchar(50)
AS
BEGIN
DECLARE @strCorreos AS VARCHAR(MAX);
DECLARE @dtCorreos TABLE(correo VARCHAR(100));
DECLARE @intIndiceInicio AS INT;
DECLARE @intIndiceFin AS INT;
DECLARE @strCorreo AS VARCHAR(100);
DECLARE @ultimo AS INT;
SET @intIndiceInicio = 1;
SET @intIndiceFin = 0;
SET @ultimo = 0;
SELECT @strCorreos = [correo] from (
SELECT correo
FROM CORREOS
WHERE OPCIONID = @OpcionId
) as x
;
WHILE @intIndiceFin <> LEN(@strCorreos)
BEGIN
SET @intIndiceFin = CHARINDEX(',', @strCorreos, @intIndiceInicio);
IF @intIndiceFin = 0
BEGIN
SET @IntIndiceFin = LEN(@strCorreos);
SET @ultimo = 1
END;
IF @ultimo = 0
SET @strCorreo = SUBSTRING(@strCorreos, @intIndiceInicio, (@intIndiceFin - @intIndiceInicio));
ELSE
SET @strCorreo = SUBSTRING(@strCorreos, @intIndiceInicio, ((@intIndiceFin - @intIndiceInicio) + 1));
INSERT INTO @dtCorreos
VALUES(@strCorreo);
SET @intIndiceInicio = (@intIndiceFin + 1);
END;
SELECT *
FROM @dtcorreos
ORDER BY correo;
END;
No hay comentarios:
Publicar un comentario