Migrate Blob Columns To File System With Copy

PHOTO EMBED

Thu Dec 15 2022 11:49:00 GMT+0000 (Coordinated Universal Time)

Saved by @HristoT #c#

select max(f)
from (with t as (select e.patlabexamid,
                        e.patlabexamname,
                        e.patlabexamdate,
                        f.patlabexamid,
                        f.patlabexamfilename,
                        replace(substring(f.patlabexamfilename from '\..*'), '.', '') as file_extention,
                        fol.path,
                        f.documentid,

                        format(
                                'copy (select encode(s.documentblob, ''hex'') from documents s where s.documentid = %s) to ''%s''',
                                f.documentid, fol.path)                               as query
                 from g_pat_lab_exam_files f
                          join g_pat_lab_exams e on f.patlabexamid = e.patlabexamid
                          join lateral ( select '/j_storage/' ||
                                                to_char(e.patlabexamdate, 'YYYY_MM/') || f.documentid || '.hex'
                                                    as path
                                         from global_settings s
                                         where globalsettingid = 330) as fol on true

                 where f.file_id is null
           and f.patlabexamfileid between 1 and 43000
)
      select f_execute_test(query) f

      from t) as t
;
content_copyCOPY