IF EXISTS ( SELECT * 
            FROM   sysobjects 
            WHERE  id = object_id(N'dbo.CF_Reportes_User_Validar') 
                   and type = 'FN')
BEGIN
    DROP FUNCTION dbo.CF_Reportes_User_Validar
END
GO


CREATE FUNCTION dbo.CF_Reportes_User_Validar(@pId_Reporte VARCHAR(50), @pId_Usuario INT)
RETURNS TINYINT
AS
BEGIN

DECLARE @Cant INT

SET @Cant = (SELECT COUNT(*) FROM TAB_REPORTES_USERS WHERE ID_REPORTE = @pId_Reporte)

IF ISNULL(@Cant,0) = 0 RETURN 1

SET @Cant = (SELECT COUNT(*) FROM TAB_REPORTES_USERS WHERE ID_REPORTE = @pId_Reporte AND ID_USUARIO = @pId_Usuario)

IF ISNULL(@Cant,0) > 0 RETURN 1



RETURN 0
END