CREATE FUNCTION DBO.GetArrangementLineDepartmentStr (@AArrangementLineNumber INT, @AAddDelimiter BIT, @AGetKeys BIT)  
/*  
  Created by: Anders Vindal 2007 0410 used in TArrangementLinesView
*/
RETURNS VARCHAR(1000) AS  
BEGIN 
  DECLARE  @Result VARCHAR (1000)
  SET @Result = ''

  DECLARE @DepartmentNumber CHAR(1)
  DECLARE @PK_Department INT

  DECLARE db_cur CURSOR READ_ONLY FAST_FORWARD FOR
    SELECT D.DepartmentNumber, D.PK_Department 
    FROM Departments D WITH (NOLOCK)
    INNER JOIN ArrangementLineDepartmentXTab A WITH (NOLOCK) ON A.FK_DepartmentNumber =  D.PK_Department 
    AND A.FK_ArrangementLineNumber = @AArrangementLineNumber
  OPEN db_cur

  FETCH NEXT FROM db_cur INTO @DepartmentNumber, @PK_Department
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    IF @AAddDelimiter = 1 AND @Result <> ''
      SET @Result = @Result + ','

    IF @AGetKeys = 1
      SET @Result = @Result + CONVERT(VARCHAR(3),@PK_Department)
    ELSE
      SET @Result = @Result + @DepartmentNumber

    FETCH NEXT FROM db_cur INTO @DepartmentNumber, @PK_Department
  END

  RETURN @Result
END

