CREATE FUNCTION dbo.GetWareRoomString(@AWareNumber  int, @GetKeys bit)
--Created AV 2008 0514

/*
  The function is used in Management --->> WareGroups- ->> Wares -- >>Advanced -->>Room  numbers

*/

RETURNS VARCHAR(8000)
AS
BEGIN

  DECLARE  @Result VARCHAR (8000)
  DECLARE  @Text Varchar (5)
  DECLARE  @PK_RoomNumber int

  DECLARE Ware_cursor CURSOR FOR

  SELECT Rooms.Number, Rooms.RoomNumber FROM Rooms WITH (NOLOCK)
  INNER JOIN WareRoomXtab WITH (NOLOCK) ON WareRoomXtab.FK_RoomNumber =  Rooms.RoomNumber AND
  WareRoomXtab.FK_WareNumber = @AWareNumber 
  ORDER BY Rooms.Number

  OPEN Ware_cursor
  FETCH NEXT FROM Ware_cursor INTO @Text, @PK_RoomNumber

  SET @Result = ''
  WHILE @@fetch_status = 0
  BEGIN
    IF @Result <> ''
      SET @Result = @Result + ','

   IF @GetKeys = 1
    SET @Result = @Result + CONVERT(VARCHAR(6),@PK_RoomNumber)
   ELSE
    SET @Result = @Result + @Text

    FETCH NEXT FROM Ware_cursor INTO @Text, @PK_RoomNumber
  END

  CLOSE Ware_cursor
  DEALLOCATE Ware_cursor

  RETURN @Result

end

