Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General »

IF en Stored Prcedures?

Estas en el tema de IF en Stored Prcedures? en el foro de Bases de Datos General en Foros del Web. Q onda? como estan? Pues yo estoy un tanto mal, he estado buscando alguna manera de utilizar IF en procedimientos Almacenados pero creo q lo ...
  #1 (permalink)  
Antiguo 07/06/2005, 10:27
 
Fecha de Ingreso: abril-2004
Ubicación: Mérida, Yucatán
Mensajes: 35
Antigüedad: 20 años
Puntos: 0
IF en Stored Prcedures?

Q onda? como estan?

Pues yo estoy un tanto mal, he estado buscando alguna manera de utilizar IF en procedimientos Almacenados pero creo q lo estoy haciendo mal

de Igual manera estuve buscando algo asi como un switch o case y tampoco encontre nada,

debe haber alguna manera de hacerlo, no puedo creer q sql server no haya contemplado esta situación, o si?

pongo algo de mi SP pa ver si pueden ayudarme, por favor

''''''''''''

CREATE PROCEDURE [sp_repVentaVendRSuc1]
(
@fechaI [nvarchar] (10),
@fechaF [nvarchar] (10),
@sucursal [nvarchar] (2)

)
AS
create table #temp
(
vendedor nvarchar (5),
nomvendedor nvarchar (150),
totpzas float,
totkgs float,
importe float,
contado float,
credito float
)

IF @SUCURSAL = '1'

INSERT INTO #temp select * from tbl1 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl2 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl3 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and ncvesuc = @sucursal


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''' aki quiero q termine el primer if
IF @SUCURSAL <> '1'

INSERT INTO #temp select * from tbl1 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl2 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl3 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and ncvesuc = @sucursal


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''' aki quiero q termine el segundo if


SELECT * from #temp
group by nomvendedor,vendedor
order by vendedor

'''''''''''''

Como cierro el if,o q puedo ahcer en vez del if?

espero q me puedan ayudar !!!

O si no se pude por favor, repondan, gracias...
__________________
"La inteligencia consiste no solo en el conocimiento, sino en la destreza de aplicar los conocimientos en la práctica." :pensando: :pensando:

Aristóteles
  #2 (permalink)  
Antiguo 07/06/2005, 11:48
 
Fecha de Ingreso: abril-2005
Mensajes: 19
Antigüedad: 19 años
Puntos: 0
Hola, disculpa que no tenga tanto tiempo para analizar tu caso pero te dejo en ejemplo de como lo hago yo

Ojala te sirva

Código:
-- AUTOR: GVV. OBJETO: Eliminar una venta, su detalle, y los abonos en caso de ser un crédito
-- También Actualiza lotes

ALTER  PROCEDURE Elimina_Venta 
(@Venta INT, @Sucursal INT, @Band INT, @Motivo nvarchar(150) = null, @Fecha datetime = null)
/*@Band = 1 Elimina la venta completamente
  @Band = 0 Reactualiza lotes, no elimina encabezado de venta pero pone status en 2 (Cancelado) */
AS
DECLARE @Cant INT, @Prod NVARCHAR(4), @Compra INT

DECLARE	DetVta CURSOR 
		FOR SELECT DetConCan, CatProIde, CatComIde FROM DetVtaCon WHERE VtaConIde = @Venta
OPEN DetVta
	FETCH NEXT FROM DetVta INTO @Cant, @Prod, @Compra

WHILE (@@fetch_status <> -1)
BEGIN
	IF (@@fetch_status <> -2)
	BEGIN
		UPDATE TblCatLot
		SET CatLotCan = CatLotCan + @Cant
		WHERE CatLotCve = @Prod + '-' + CAST(@Compra AS NVARCHAR)
		AND CatSucIde = @Sucursal
	END
	FETCH NEXT FROM DetVta INTO @Cant, @Prod, @Compra
END

CLOSE DetVta
DEALLOCATE DetVta

if (@Band = 1)
	BEGIN
		DELETE FROM DetVtaCon WHERE VtaConIde = @Venta	--Se Borra el detalle de la venta

		DELETE FROM TblCatAbo WHERE CatVtaIde = @Venta	--Se Borra cualquier abono en caso de ser
														--un crédito
														
		DELETE FROM TblVtaCon Where Id = @Venta			--Se Borra el encabezado de la venta
	END
ELSE IF (@Band = 0)
	BEGIN
		UPDATE TblVtaCon 
		SET VtaConSta = 2, VtaConObs = VtaConObs + ' *** ' + @Motivo, FecMovCan = @Fecha
		WHERE Id = @Venta
	END
Saludos
  #3 (permalink)  
Antiguo 07/06/2005, 12:13
Avatar de cableh  
Fecha de Ingreso: diciembre-2004
Mensajes: 54
Antigüedad: 19 años, 4 meses
Puntos: 0
Hola reneac79,

En cuanto al if:

IF @SUCURSAL = '1'
BEGIN
.
.
.
END

Y del case te pongo la estructura (copiada de los libros en pantalla):

Función CASE sencilla:
CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

Función CASE de búsqueda:

CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

Salu2.
  #4 (permalink)  
Antiguo 07/06/2005, 13:41
 
Fecha de Ingreso: abril-2004
Ubicación: Mérida, Yucatán
Mensajes: 35
Antigüedad: 20 años
Puntos: 0
gracias por responder.
probare esa solucion
__________________
"La inteligencia consiste no solo en el conocimiento, sino en la destreza de aplicar los conocimientos en la práctica." :pensando: :pensando:

Aristóteles
  #5 (permalink)  
Antiguo 07/06/2005, 13:45
 
Fecha de Ingreso: abril-2004
Ubicación: Mérida, Yucatán
Mensajes: 35
Antigüedad: 20 años
Puntos: 0
Gracias por responder, Cableh...
__________________
"La inteligencia consiste no solo en el conocimiento, sino en la destreza de aplicar los conocimientos en la práctica." :pensando: :pensando:

Aristóteles
  #6 (permalink)  
Antiguo 07/06/2005, 15:11
 
Fecha de Ingreso: marzo-2005
Ubicación: Caracas; Junquito
Mensajes: 1
Antigüedad: 19 años, 1 mes
Puntos: 0
A Ver Si Te Sirve

HOLA ME PARESE QUE SI EMPIEZAS CON "IF" DEBES SEGUIR CON CON "ELSE", EN CASO DE QUE AYA OTRA CONDICION, Y PARA TERMINAR EL CICLO DE CONDICIONES DEBES TERMINAR CON "END IF"

AL NO CUMPLIRSE LA PRIMERA CONDICION,SE COMPLIRIA LA OTRA O PUEDE DARSE EL CASO DE Q NINGUNA SE CUMPLA Y SE CULMINA EL CICLO CON "END IF"

SI NO TE RESULTA DISCULPA POR NO PODER AYUDARTE HICE LO QUE PUDE...






Cita:
Iniciado por reneac79
Q onda? como estan?

Pues yo estoy un tanto mal, he estado buscando alguna manera de utilizar IF en procedimientos Almacenados pero creo q lo estoy haciendo mal

de Igual manera estuve buscando algo asi como un switch o case y tampoco encontre nada,

debe haber alguna manera de hacerlo, no puedo creer q sql server no haya contemplado esta situación, o si?

pongo algo de mi SP pa ver si pueden ayudarme, por favor

''''''''''''

CREATE PROCEDURE [sp_repVentaVendRSuc1]
(
@fechaI [nvarchar] (10),
@fechaF [nvarchar] (10),
@sucursal [nvarchar] (2)

)
AS
create table #temp
(
vendedor nvarchar (5),
nomvendedor nvarchar (150),
totpzas float,
totkgs float,
importe float,
contado float,
credito float
)

IF @SUCURSAL = '1'

INSERT INTO #temp select * from tbl1 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl2 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl3 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and ncvesuc = @sucursal


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''' aki quiero q termine el primer if
IF @SUCURSAL <> '1'

INSERT INTO #temp select * from tbl1 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl2 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and tbl1.ncvesuc = @sucursal

INSERT INTO #temp select * from tbl3 where fecha between convert(datetime,@fechaI,103) and convert(datetime,@fechaF,103) and ncvesuc = @sucursal


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''' aki quiero q termine el segundo if

SELECT * from #temp
group by nomvendedor,vendedor
order by vendedor

'''''''''''''

Como cierro el if,o q puedo ahcer en vez del if?

espero q me puedan ayudar !!!

O si no se pude por favor, repondan, gracias...
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:57.