SELECT * FROM T_BD_STOCK
SELECT * FROM T_BD_STOCKFLEXITEM
SELECT * FROM T_BD_STOCKFLEXDETAIL
SELECT * FROM T_BAS_FLEXVALUES
SELECT * FROM T_BAS_FLEXVALUESENTRY
SELECT * FROM T_BAS_FLEXVALUESENTRY_L
SELECT * FROM T_BD_FLEXVALUESCOM
SELECT * FROM T_BAS_FLEXVALUESDETAIL

SELECT
    t1.FNUMBER
    ,t1_l.FNAME
    ,t0.FMATERIALID
    ,t0.FSTOCKID
    ,t0.FSTOCKLOCID
    ,stuff((select CAST(t2.FF100001 AS varchar) +'.'+ CAST(t2.FF100002 AS varchar) FROM T_BAS_FLEXVALUESDETAIL t2 WHERE t2.FID = t0.FSTOCKLOCID for xml path('')),1,1,'')
FROM T_STK_INVENTORY t0
    LEFT JOIN T_BD_MATERIAL t1 on t1.FMATERIALID = t0.FMATERIALID
    LEFT JOIN T_BD_MATERIAL_L t1_l on t1_l.FMATERIALID = t0.FMATERIALID AND t1_l.FLOCALEID = 2052
    --LEFT JOIN T_BAS_FLEXVALUESDETAIL t2 on t2.FID = t0.FSTOCKLOCID
WHERE
    t0.FSTOCKLOCID > 0

DECLARE @columns NVARCHAR(MAX), @sql NVARCHAR(MAX);
SELECT @columns = COALESCE(@columns + ',', '') + CONCAT('t0.',t0.FFLEXNUMBER)
FROM T_BAS_FLEXVALUES t0
WHERE t0.FDOCUMENTSTATUS = 'C' AND t0.FFORBIDSTATUS = 'A'

SET @sql = N'
SELECT
    t0.FID
    ,CONCAT(' + @columns + ' +''.'', '''')
FROM T_BAS_FLEXVALUESDETAIL t0

';

EXEC sp_executesql @sql;