CREATE FUNCTION [dbo].[_Brk_Eds_Muavin_Kod_Birlestir_Kdv]
(
    @fisno VARCHAR(20),
    @aykodu SMALLINT,
    @subekodu SMALLINT,
    @kdvkod VARCHAR(10)
)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @kod VARCHAR(10);
    DECLARE @sonuc VARCHAR(50) = '';
    DECLARE CRSBORC CURSOR FOR
    SELECT DISTINCT
           LEFT(HES_KOD, 3)
    FROM TBLMUHFIS  (NOLOCK)
    WHERE FISNO = @fisno
          AND AY_KODU = @aykodu
          AND SUBE_KODU = @subekodu
          AND LEFT(HES_KOD, 3) = @kdvkod;
    OPEN CRSBORC;
    FETCH NEXT FROM CRSBORC
    INTO @kod;
    WHILE @@FETCH_STATUS <> -1
    BEGIN
        IF @sonuc <> ''
            SET @sonuc = ',' + @kod;

        IF @sonuc = ''
            SET @sonuc = @kod;
        FETCH NEXT FROM CRSBORC
        INTO @kod;
    END;
    CLOSE CRSBORC;
    DEALLOCATE CRSBORC;
    RETURN @sonuc;
END;