Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/12/2012, 14:04
leo_acn
 
Fecha de Ingreso: diciembre-2012
Mensajes: 83
Antigüedad: 11 años, 4 meses
Puntos: 4
Pregunta Cursor para ordenar datos horizontalmente

Hola a todos les cuento mi problema, trabajo con dos tablas, la tabla prueba y la tabla libro. Para que es quede mas claro, a tabla prueba tiene el campo llamado "rut" que es un identificador y un campo llamado "telcontacto" que son números telefónicos. la tabla libro tiene los campos del identificador "rut" y los teléfonos del 1 al 20.

Tabla Prueba
origen varchar(15)
rut decimal(9)
telconsolidado char(18)
fecha datetime(8)

Tabla Libro
rut nvarchar(50)
fono1 nvarchar(50)
fono2 nvarchar(50)
...
fono20 nvarchar(50)

En la tabla prueba pueden haber rut que tengan mas de un teléfono, hasta 10 casi siempre, y lo que necesito hacer es tomar el rut y el telñefono de la tabla prueba y guardarlos en la tabla libro, perooo guardar solo un campo rut e incluirle los telefonos hacia la derecha, por ejemplo si tenemos los siguientes datos

rut fono
123 25632
123 25874
254 1254
586 1257

Esto debería quedar asi en la tabla libro:

rut fono1 fono2..... fono20
123 25632 25874
254 1254
586 1257

Ahora bien, para hacer esto la mejor solución que pense es un cursor el que les dejo a continuación para que me digan donde me estoy equivocando porque lo ejecuto, pero no me guarda ningun dato en la tabla libro, algun consejo por favor? gracias :)




-- Declaracion de variables para el cursor
DECLARE @rut decimal,
@fono char(18)

-- Declaración del cursor
DECLARE Fono CURSOR FOR
SELECT rut, telconsolidado FROM prueba

-- Apertura del cursor
OPEN Fono

-- Lectura de la primera fila del cursor
FETCH Fono INTO @rut, @fono
WHILE (@@FETCH_STATUS = 0 )
BEGIN
select rut from prueba where rut = @rut
if @@rowcount = 0
insert into libro (rut, fono1) values (@rut, @fono)
else
begin
select fono2 from libro where rut = @rut and fono2 > 0
if @@rowcount = 0
update libro set fono2 = @fono where rut = @rut
else
begin
select fono3 from libro where rut = @rut and fono3 > 0
if @@rowcount = 0
update libro set fono3 = @fono where rut = @rut
else
begin
select fono4 from libro where rut = @rut and fono4 > 0
if @@rowcount = 0
update libro set fono4 = @fono where rut = @rut
else
begin
select fono5 from libro where rut = @rut and fono5 > 0
if @@rowcount = 0
update libro set fono5 = @fono where rut = @rut
else
begin
select fono6 from libro where rut = @rut and fono6 > 0
if @@rowcount = 0
update libro set fono6 = @fono where rut = @rut
else
begin
select fono7 from libro where rut = @rut and fono7 > 0
if @@rowcount = 0
update libro set fono7 = @fono where rut = @rut
else
begin
select fono8 from libro where rut = @rut and fono8 > 0
if @@rowcount = 0
update libro set fono8 = @fono where rut = @rut
else
begin
select fono9 from libro where rut = @rut and fono9 > 0
if @@rowcount = 0
update libro set fono9 = @fono where rut = @rut
else
begin
select fono10 from libro where rut = @rut and fono10 > 0
if @@rowcount = 0
update libro set fono10 = @fono where rut = @rut
else
begin
select fono11 from libro where rut = @rut and fono11 > 0
if @@rowcount = 0
update libro set fono11 = @fono where rut = @rut
else
begin
select fono12 from libro where rut = @rut and fono12 > 0
if @@rowcount = 0
update libro set fono12 = @fono where rut = @rut
else
begin
select fono13 from libro where rut = @rut and fono13 > 0
if @@rowcount = 0
update libro set fono13 = @fono where rut = @rut
else
begin
select fono14 from libro where rut = @rut and fono14 > 0
if @@rowcount = 0
update libro set fono14 = @fono where rut = @rut
else
begin
select fono15 from libro where rut = @rut and fono15 > 0
if @@rowcount = 0
update libro set fono15 = @fono where rut = @rut
else
begin
select fono16 from libro where rut = @rut and fono16 > 0
if @@rowcount = 0
update libro set fono16 = @fono where rut = @rut
else
begin
select fono17 from libro where rut = @rut and fono17 > 0
if @@rowcount = 0
update libro set fono17 = @fono where rut = @rut
else
begin
select fono18 from libro where rut = @rut and fono18 > 0
if @@rowcount = 0
update libro set fono18 = @fono where rut = @rut
else
begin
select fono19 from libro where rut = @rut and fono19 > 0
if @@rowcount = 0
update libro set fono19 = @fono where rut = @rut
else
update libro set fono20 = @fono where rut = @rut
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
FETCH Fono INTO @rut, @fono
END

-- Cierre del cursor
CLOSE Fono

-- Liberar los recursos
DEALLOCATE Fono