CREATE FUNCTION dbo.MCC_GetPDAMaintenanceStaff (@HotelNumber INT) RETURNS @Result TABLE (
  UserIDNumber INT,
  FirstName VARCHAR(50), 
  LastName VARCHAR(50)
) AS
BEGIN
  INSERT INTO @Result
  SELECT DISTINCT PersonelDepartmentsXtab.UserIDNumber, PERSONEL.FirstName, PERSONEL.LastName
  FROM PersonelDepartmentsXtab WITH (NOLOCK) 
  INNER JOIN DEPARTMENTS WITH (NOLOCK) ON PersonelDepartmentsXtab.FK_Department = DEPARTMENTS.PK_Department 
  INNER JOIN PERSONEL WITH (NOLOCK) ON PERSONEL.UserIDNumber = PersonelDepartmentsXtab.UserIDNumber
  WHERE ISNULL(DEPARTMENTS.HotelNumber,@HotelNumber) = @HotelNumber
  AND PERSONEL.UserTypeNumber = 1 
  AND PERSONEL.ActivePerson = 1
  AND PERSONEL.FirstName + PERSONEL.LastName <> ''
  AND ISNULL(PERSONEL.MarkAsDeleted, 0) = 0
--  AND CHARINDEX (','+ CAST( Personel.UseridNumber AS VARCHAR) + ',' , (dbo.GetHotelSetupKeyValue ('mccUserAvailableForTaskCreationFromPDA', (SELECT CustomerNumber FROM Customer WITH (NOLOCK) WHERE HotelNumber = @HotelNumber)))) > 0
  AND Personel.UserIdNumber IN (SELECT DISTINCT Number FROM iter_intlist_to_table(dbo.GetHotelSetupKeyValue ('mccUserAvailableForTaskCreationFromPDA', (SELECT CustomerNumber FROM Customer WITH (NOLOCK) WHERE HotelNumber = @HotelNumber))))
  RETURN
END

