CREATE FUNCTION dbo.GetHotelCustomerNumberFromDepartment(@APK_Department INT) RETURNS INT AS
BEGIN
  DECLARE @Res INT

  SELECT @Res = CustomerNumber
  FROM Customer WITH (NOLOCK)
  INNER JOIN Departments WITH (NOLOCK) ON Departments.HotelNumber = Customer.HotelNumber
    AND Departments.PK_Department = @APK_Department
  WHERE Customer.CustomerTypeNumber = 7
  AND Customer.HotelNumber >= 0 
  AND ISNULL(Customer.MarkAsDeleted, 0) = 0

  RETURN @Res 
END