DROP PROCEDURE CargarTablasTemporalesProc
GO
CREATE PROCEDURE CargarTablasTemporalesProc
(
	@CORRNBR	int,
	@CUSTIDSS       UD_CODCLIENT
)
AS
BEGIN
    BEGIN TRAN

    --GRABANDO EN TABLA DE PLACAS DE CLIENTES
    INSERT 	INTO 	TMP_OP_CUSTBADGE(CORRNBR,BADGE)
		SELECT 	@CORRNBR,BADGE
		FROM	OP_CUSTBADGE
		WHERE	CUSTIDSS=@CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en TMP_OP_CUSTBADGE'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE TARJETAS DE CLIENTES
    INSERT 	INTO 	TMP_OP_CUSTCARD(CORRNBR,CARDPASSWORD,EXPIREDATE,DESCRCUSTCARD)
		SELECT 	@CORRNBR,CARDPASSWORD,EXPIREDATE,DESCRCUSTCARD
		FROM	OP_CUSTCARD
		WHERE	CUSTIDSS=@CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en TMP_OP_CUSTCARD'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE GARANTIAS DE CLIENTES
    INSERT 	INTO 	TMP_CUSTWARRANTY(CORRNBR,CUSTWARRANTYTYPEID,TOTGAR,DESCRGAR,EXPIREDATE,CURYID)
		SELECT 	@CORRNBR,CUSTWARRANTYTYPEID,TOTGAR,DESCRGAR,EXPIREDATE,CURYID
		FROM	CUSTWARRANTY
		WHERE	CUSTIDSS=@CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en TMP_CUSTWARRANTY'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    --GRABANDO EN TABLA DE CREDITOS DE CLIENTES
    INSERT 	INTO 	TMP_OP_CUST_LIMIT(CORRNBR,SITEID,PORCENTLIMITED,CREDLIMITED,CURRBAL,PORCENTSURPLUS,MTOSURPLUS)
		SELECT 	@CORRNBR,SITEID,PORCENTLIMITED,CREDLIMITED,CURRBAL,PORCENTSURPLUS,MTOSURPLUS
		FROM	OP_CUST_LIMIT
		WHERE	CUSTIDSS=@CUSTIDSS

    IF (@@error!=0)
    BEGIN
        RAISERROR  20000 'Error Grabando en TMP_OP_CUST_LIMIT'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END

 