CREATE FUNCTION dbo.GetRateLevelProducts (@ARateLevelId INT) RETURNS VARCHAR(8000) AS
BEGIN
  DECLARE @Result VARCHAR(8000)
  SET @Result = ''

  DECLARE @ProductName VARCHAR(50)

  DECLARE db_cur CURSOR READ_ONLY FAST_FORWARD FOR
    SELECT Products.ProductName
    FROM Products WITH (NOLOCK)
    WHERE FKRateLevelId = @ARateLevelId
  OPEN db_cur

  FETCH NEXT FROM db_cur INTO @ProductName
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    IF @Result <> ''
      SET @Result = @Result + ','
    SET @Result = @Result + @ProductName

    FETCH NEXT FROM db_cur INTO @ProductName
  END

  RETURN @Result
END