CREATE FUNCTION dbo.GetRateLevelRoomTypeStr (@ARateLevelId INT, @AGetKeys BIT) RETURNS VARCHAR(8000) AS
BEGIN
  DECLARE @RoomTypeName VARCHAR(50)
  DECLARE @RoomTypeNumber INT
  DECLARE @res VARCHAR(8000)
  SET @res = ''
  
  DECLARE db_cur CURSOR FOR
    SELECT RoomTypes.RoomTypeNumber, RoomTypes.Type 
    FROM RoomTypes WITH (NOLOCK)
    INNER JOIN RateLevelRoomTypeXTab WITH (NOLOCK) ON RateLevelRoomTypeXTab.FKRoomTypeNumber = RoomTypes.RoomTypeNumber
      AND RateLevelRoomTypeXTab.FKRateLevelId = @ARateLevelId
  OPEN db_cur
  
  FETCH NEXT FROM db_cur INTO @RoomTypeNumber, @RoomTypeName
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    IF @AGetKeys = 1
      SET @res = @res + CONVERT(VARCHAR, @RoomTypeNumber) + ','
    ELSE
      SET @res = @res + @RoomTypeName + ', '
    FETCH NEXT FROM db_cur INTO @RoomTypeNumber, @RoomTypeName
  END
  CLOSE db_cur
  DEALLOCATE db_cur
  
  IF @res <> ''
  BEGIN
    IF @AGetKeys = 1
      SET @res = SUBSTRING(@res, 1, LEN(@res))
    ELSE
      SET @res = SUBSTRING(@res, 1, LEN(@res)-1)
  END  
  
  RETURN @res
END