SQL Server - CICLOS EN SQL SERVER URGENTE

 
Vista:

CICLOS EN SQL SERVER URGENTE

Publicado por jhonatan (1 intervención) el 26/08/2013 03:35:20
TENGO UNA TABLA CON LOS SIGUIENTES CAMPOS:

create table ejemplo (
id int identity not null,
nombre varchar (50)not null,
edad int not null
)

NECESITO CREAR UN PROCEDIMIENTO ALMACENADO EN EL CUAL TENGO CICLOS:

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
create procedure spejemplo
@inicio int,
@final int
as
begin
	declare @nombre varchar(50)
	declare @edad int
	declare @a int
 
	set @inicio =1
	set @a=1
	set @nombre='Jhonatan'
	set @edad=20
 
	while @inicio <= @final
	begin
		insert into ejemplo values (@nombre,@edad)
 
	if @nombre='Jhonatan' set @nombre='Marina'
	else if @nombre='Marina' set @nombre='Pedro'
	else if @nombre='Pedro' set @nombre='Temistocles'
	else if @nombre='Temistocles' set @nombre='Chimoltrufia'
	else if @nombre='Chimoltrufia' set @nombre='Claudio'
	else if @nombre='Claudio' set @nombre='Casimiro'
 
	if @edad=20 set @edad=30
	else if @edad=30 set @edad=40
	else if @edad=40 set @edad=65
	else if @edad=65 set @edad=36
	else if @edad=36 set @edad=17
	else if @edad=17 set @edad=42
 
	set @inicio=@inicio+1
end
end


NECESITO QUE DESPUES DE ESTO EL CICLO ME TOME ESTOS DATOS
NO SE SI UTILIZAR UN SWITCH Y DE Q MANERA
1
2
3
4
5
6
7
8
9
begin
	if @nombre='Casimiro' set @nombre='Chimoltrufia'
	else if @nombre='Chimoltrufia' set @nombre='Pedro'
	else if @nombre='Pedro' set @nombre='Jhonatan'
 
	if @edad=42 set @edad=36
	else if @edad=36 set @edad=40
	else if @edad=40 set @edad=20
	end


DE TAL MANERA QUE QUEDE ASI AL UTILIZAR LA SENTENCIA
EXEC SPEJEMPLO 1,10

1
2
3
4
5
6
7
8
9
10
11
12
Id	Nombre  	Edad
1	Jhonatan	 20
2	marina    	 30
3	pedro	         40
4	Temistocles	 65
5	Chimoltrufia	 36
6	claudio	         17
7	casimiro	 42
 
8	Chimoltrufia	 36
9	Pedro	         40
10	jhonatan  	 20


LE AGRADEZCO SU AYUDA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Geri

CICLOS EN SQL SERVER URGENTE

Publicado por Geri (17 intervenciones) el 28/08/2013 08:48:32
jhonatan - este procedimiento no tiene mucho sentido, pero supongo que es un ejercicio o quieres probar como funciona el While en este caso..

Si el dicho código ("if @nombre='Casimiro'") debe ser parte del bucle- pon lo antes el primer 'end',
y si debe estar fuera del bucle - pon lo entre los dos 'end'
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar