Cursor

02/10/2003 - 19:56 por Luis Gutrierrez | Informe spam
Tengo un Store Procedure en donde implemento un cursor,
pero este no me arroja los resultados que yo espero,
alguine me podria dar una mano y explicarme en donde estoy
fallando,
Agradeciendo de antemano toda la ayuda que me pudiesen
prestar, me despido


Luis Mizraim Gutierrez Pedroza
Analista de Sistemas
INFRA S.A.
lgutierrez@infrarad.com
www.infrarad.com
Caracas/Venezuela


CREATE PROCEDURE SP_IR_CurDistInv
AS
DECLARE @Item char(10), @Itemdesc1 char(15), @Itemdesc2
char(15), @Loc char(3), @Doctype char(1), @Docord char(8),
@User char(10), @quantity decimal(9), @Oldquantity
decimal(9), @Trxdt int, @Docdt int,
@Oldunitcost decimal(9), @unitcost decimal(9),
@newcost decimal(9), @Trxtm int, @Lev smallint,
@Entrada int, @Salida int

DECLARE Distribucion_Cursor CURSOR FOR
SELECT A.item_no, B.item_desc_1, B.item_desc_2, A.loc,
A.doc_type, A.doc_ord_no,
A.[user_name], A.quantity, A.old_quantity,
A.trx_dt, A.doc_dt, A.old_unit_cost,
A.unit_cost, A.new_unit_cost, A.trx_tm, A.lev_no
FROM IMINVTRX_SQL AS A
INNER JOIN IMITMIDX_SQL AS B ON A.item_no = B.item_no
WHERE A.item_no BETWEEN 'AAAAAAAAAA' AND 'ZZZZZZZZZZ'
AND A.doc_dt BETWEEN '20030901' AND '20030903'
AND A.loc BETWEEN '400' AND '400'
ORDER BY A.loc, A.item_no, A.trx_dt, A.trx_tm

OPEN Distribucion_Cursor

FETCH NEXT FROM Distribucion_Cursor
INTO @Item, @Itemdesc1, @Itemdesc2, @Loc, @Doctype,
@Docord,
@User, @quantity, @Oldquantity, @Trxdt, @Docdt,
@Oldunitcost, @unitcost, @newcost, @Trxtm, @Lev

WHILE @@FETCH_STATUS=0
BEGIN
IF @Lev<>1 and @Doctype='R'
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
IF @Lev=0 AND @Doctype<>'P' AND @Doctype<>'A'
AND @Doctype<>'R' AND @Doctype<>'O' AND @Doctype<>'C'
BEGIN
SET @Salida=@Quantity
SET @Entrada=0
IF @Doctype='Q' and @Quantity<0
BEGIN
SET @Salida=@Quantity*-1
SET @Entrada=0
END
ELSE
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
END
ELSE
IF @Doctype<>'P' AND @Doctype<>'A' AND
@Doctype<>'O' AND @Doctype<>'C'
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
ELSE
IF @Lev=1 AND @Doctype='I'
BEGIN
SET @Salida=@Quantity
SET @Entrada=0
END
END




PRINT 'Item: ' + @Item
PRINT 'Descripcion: ' + @Itemdesc1 + ' ' + @Itemdesc2
PRINT 'Tipo Doc: ' + @Doctype
PRINT 'Numero Doc: ' + @Docord
PRINT 'Usuario: ' + @User
PRINT @Entrada
PRINT @Salida
PRINT @Trxdt
PRINT @Docdt
PRINT @unitcost




CLOSE Distribucion_Cursor
DEALLOCATE Distribucion_Cursor
 

Leer las respuestas

#1 Liliana Sorrentino
02/10/2003 - 21:01 | Informe spam
Luis,
sin cursor podés hacer lo mismo, poniendo las condiciones de suma de
cantidades de entrada y salida con CASE:
Liliana.

SELECT Item = A.item_no, Descripcion = B.item_desc_1 + ' ' + B.item_desc_2,
TipoDoc = A.doc_type, NumeroDoc = A.doc_ord_no, Usuario = A.[user_name],
A.trx_dt, A.doc_dt, A.unit_cost
Entrada = SUM(CASE WHEN A.lev_no <> 1 and A.doc_type = 'R' THEN A.quantity
WHEN A.lev_no = 0 and A.doc_type NOT IN ('P', 'A', 'R', 'O', 'C')
THEN 0
WHEN A.lev_no = 0 and A.doc_type = 'Q' AND A.quantity < 0 THEN 0
END,
Salida = SUM(CASE WHEN A.lev_no <> 1 and A.doc_type = 'R' THEN 0
WHEN A.lev_no = 0 and A.doc_type = 'R' THEN A.quantity
WHEN A.lev_no = 0 and A.doc_type = 'Q' AND A.quantity < 0 THEN
A.quantity * -1
END
FROM IMINVTRX_SQL AS A
INNER JOIN IMITMIDX_SQL AS B ON A.item_no = B.item_no
WHERE A.item_no BETWEEN 'AAAAAAAAAA' AND 'ZZZZZZZZZZ' AND
A.doc_dt BETWEEN '20030901' AND '20030903' AND
A.loc BETWEEN '400' AND '400'
GROUP BY A.item_no, B.item_desc_1, B.item_desc_2, A.doc_type,
A.doc_ord_no, A.[user_name], A.trx_dt, A.doc_dt, A.unit_cost
ORDER BY A.loc, A.item_no, A.trx_dt, A.trx_tm

"Luis Gutrierrez" escribió en el mensaje
news:067801c3890e$7b460640$
Tengo un Store Procedure en donde implemento un cursor,
pero este no me arroja los resultados que yo espero,
alguine me podria dar una mano y explicarme en donde estoy
fallando,
Agradeciendo de antemano toda la ayuda que me pudiesen
prestar, me despido


Luis Mizraim Gutierrez Pedroza
Analista de Sistemas
INFRA S.A.

www.infrarad.com
Caracas/Venezuela


CREATE PROCEDURE SP_IR_CurDistInv
AS
DECLARE @Item char(10), @Itemdesc1 char(15), @Itemdesc2
char(15), @Loc char(3), @Doctype char(1), @Docord char(8),
@User char(10), @quantity decimal(9), @Oldquantity
decimal(9), @Trxdt int, @Docdt int,
@Oldunitcost decimal(9), @unitcost decimal(9),
@newcost decimal(9), @Trxtm int, @Lev smallint,
@Entrada int, @Salida int

DECLARE Distribucion_Cursor CURSOR FOR
SELECT A.item_no, B.item_desc_1, B.item_desc_2, A.loc,
A.doc_type, A.doc_ord_no,
A.[user_name], A.quantity, A.old_quantity,
A.trx_dt, A.doc_dt, A.old_unit_cost,
A.unit_cost, A.new_unit_cost, A.trx_tm, A.lev_no
FROM IMINVTRX_SQL AS A
INNER JOIN IMITMIDX_SQL AS B ON A.item_no = B.item_no
WHERE A.item_no BETWEEN 'AAAAAAAAAA' AND 'ZZZZZZZZZZ'
AND A.doc_dt BETWEEN '20030901' AND '20030903'
AND A.loc BETWEEN '400' AND '400'
ORDER BY A.loc, A.item_no, A.trx_dt, A.trx_tm

OPEN Distribucion_Cursor

FETCH NEXT FROM Distribucion_Cursor
INTO @Item, @Itemdesc1, @Itemdesc2, @Loc, @Doctype,
@Docord,
@User, @quantity, @Oldquantity, @Trxdt, @Docdt,
@Oldunitcost, @unitcost, @newcost, @Trxtm, @Lev

WHILE @@FETCH_STATUS=0
BEGIN
IF @Lev<>1 and @Doctype='R'
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
IF @Lev=0 AND @Doctype<>'P' AND @Doctype<>'A'
AND @Doctype<>'R' AND @Doctype<>'O' AND @Doctype<>'C'
BEGIN
SET @Salida=@Quantity
SET @Entrada=0
IF @Doctype='Q' and @Quantity<0
BEGIN
SET @Salida=@Quantity*-1
SET @Entrada=0
END
ELSE
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
END
ELSE
IF @Doctype<>'P' AND @Doctype<>'A' AND
@Doctype<>'O' AND @Doctype<>'C'
BEGIN
SET @Entrada=@Quantity
SET @Salida=0
END
ELSE
IF @Lev=1 AND @Doctype='I'
BEGIN
SET @Salida=@Quantity
SET @Entrada=0
END
END




PRINT 'Item: ' + @Item
PRINT 'Descripcion: ' + @Itemdesc1 + ' ' + @Itemdesc2
PRINT 'Tipo Doc: ' + @Doctype
PRINT 'Numero Doc: ' + @Docord
PRINT 'Usuario: ' + @User
PRINT @Entrada
PRINT @Salida
PRINT @Trxdt
PRINT @Docdt
PRINT @unitcost




CLOSE Distribucion_Cursor
DEALLOCATE Distribucion_Cursor




Preguntas similares