Ver Mensaje Individual
  #6 (permalink)  
Antiguo 09/07/2011, 02:26
Avatar de Joch_pa
Joch_pa
 
Fecha de Ingreso: octubre-2009
Ubicación: Pachuca De Soto, Hidalgo, Mexico, Mexico
Mensajes: 122
Antigüedad: 14 años, 5 meses
Puntos: 7
Respuesta: dividir registros de Función Escalar

mmmm mira yo tengo la siguiente funcion:


Código SQL:
Ver original
  1. CREATE FUNCTION SplitN(@campo VARCHAR(150), @clave CHAR(1))
  2.     RETURNS VARCHAR(8000)
  3. BEGIN
  4.     DECLARE @indice INT
  5.     DECLARE @indice2 INT
  6.     DECLARE @resultado VARCHAR(8000)
  7.     DECLARE @tmp VARCHAR(200)
  8.     SELECT @indice = 1
  9.     SET @resultado = ''
  10.     IF @campo IS NULL RETURN @resultado
  11.     WHILE @indice !=0
  12.     BEGIN
  13.         SELECT @indice = CHARINDEX(@clave,@campo)
  14.         SELECT @indice2 = CHARINDEX(@clave,@campo,@indice+1)
  15.         IF @indice2=0
  16.             SELECT @indice2 = len(@campo)
  17.         IF @indice != 0
  18.         BEGIN
  19.             SELECT @tmp = SUBSTRING(@campo,@indice,@indice2)
  20.             SELECT @tmp = REPLACE(@campo,@tmp,'')
  21.             SELECT @tmp = SUBSTRING(@campo,1,CHARINDEX(@clave,@campo) -1)
  22.         END
  23.         ELSE
  24.             SELECT @tmp = @campo
  25.         SET @resultado = @resultado + @tmp
  26.         SELECT @campo =  SUBSTRING(@campo,@indice2+1,len(@campo))
  27.         IF LEN(@campo) = 0 BREAK
  28.     END
  29.     RETURN @resultado
  30. END
  31. GO

ok ?


bueno, si hago lo siguiente:
Código SQL:
Ver original
  1. DROP TABLE #tabla
  2. GO
  3. CREATE TABLE #tabla
  4. (
  5.     Participantes VARCHAR(50)
  6. )
  7.  
  8. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre1,#45#Nombre2')
  9. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre3,#45#Nombre4')
  10. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre5,#45#Nombre6')
  11. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre7,#45#Nombre8')
  12. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre9')
  13. INSERT INTO #tabla (Participantes) VALUES('#123#Nombre10,#45#Nombre11,#45#Nombre12')
  14. INSERT INTO #tabla (Participantes) VALUES('Nombre13')
  15.  
  16. DECLARE @str VARCHAR(8000)
  17.     SET @str = ' select dbo.SplitN( PArticipantes, ''#'')+'','' as [Participantes] from #tabla '
  18. DECLARE @str2 VARCHAR(8000)
  19.     SET @str2 = ' SELECT substring(Participantes,1, charindex('','',Participantes)) as [Participantes] FROM ('+ @str +')A  union '
  20.     SET @str2 = @str2 +' SELECT substring(Participantes, charindex('','',Participantes)+1,len(Participantes)) as [Participantes] FROM ('+ @str +')A'
  21.  
  22. DECLARE @str3 VARCHAR(8000)
  23.     SET @str3 = ' SELECT substring(Participantes,1, charindex('','',Participantes)) as [Participantes] FROM ('+ @str2 +')A  union '
  24.     SET @str3 = @str3 +' SELECT substring(Participantes, charindex('','',Participantes)+1,len(Participantes)) FROM ('+ @str2 +')A'
  25.  
  26. DECLARE @str4 VARCHAR(8000)
  27.     SET @str4 = ' SELECT substring(Participantes,1, charindex('','',Participantes)) as [Participantes] FROM ('+ @str3 +')A  union '
  28.     SET @str4 = @str4 +' SELECT substring(Participantes, charindex('','',Participantes)+1,len(Participantes)) as [Participantes] FROM ('+ @str3 +')A'
  29.  
  30. EXEC('Select replace(Participantes,'','','''') From ('+ @str4+')B where Participantes<>''''')

Me da los resultados


Código Resultado:
Ver original
  1. Nombre1
  2. Nombre10
  3. Nombre11
  4. Nombre12
  5. Nombre13
  6. Nombre2
  7. Nombre3
  8. Nombre4
  9. Nombre5
  10. Nombre6
  11. Nombre7
  12. Nombre8
  13. Nombre9

Checala y me dices que onda, si te sirve o cualquier duda.


No es la mejor forma tal ves, pero es lo primero que se me vino a la mente.