DROP PROCEDURE CustomerDelProc
GO
CREATE PROCEDURE CustomerDelProc
(
    @CUSTIDSS         UD_CODCLIENT
)
AS
BEGIN
    BEGIN TRAN

    --ELIMINANDO REGISTROS DEL CLIENTE EN TABLA OP_CUSTBADGE
    DELETE 
    FROM OP_CUSTBADGE
    WHERE CUSTIDSS=@CUSTIDSS  

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


    --ELIMINANDO REGISTROS DEL CLIENTE EN TABLA OP_CUSTCARD
    DELETE 
    FROM OP_CUSTCARD
    WHERE CUSTIDSS=@CUSTIDSS  

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

    --ELIMINANDO REGISTROS DEL CLIENTE EN TABLA CUSTWARRANTY
    DELETE 
    FROM CUSTWARRANTY
    WHERE CUSTIDSS=@CUSTIDSS  

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

    --ELIMINANDO REGISTROS DEL CLIENTE EN TABLA OP_CUST_LIMIT
    DELETE 
    FROM OP_CUST_LIMIT
    WHERE CUSTIDSS=@CUSTIDSS  

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

    --ELIMINANDO REGISTRO EN TABLA CUSTOMER
    DELETE 
    FROM CUSTOMER
    WHERE CUSTIDSS=@CUSTIDSS   

    IF (@@error!=0 OR @@rowcount=0)
    BEGIN
        RAISERROR  20000 'Error Eliminando en CUSTOMER'
        ROLLBACK TRAN
        RETURN(1)
    
    END

    COMMIT TRAN

END
