Rabu, 05 Februari 2020

Looping Update Tanggal (Day) - PostgreSQL

Jika ingin update data tanggal, tapi hanya harinya saja yang diganti
DO $$
    Declare idaspk int;   
        DECLARE htempo int;
        DECLARE hcair int;
    cur1 CURSOR for select id, haritempo, haricair from dbadata.temp_idsimulasi_bedatanggal;   
 begin 
OPEN Cur1 ; 
Loop     


    FETCH  next from Cur1 INTO idaspk, htempo, hcair ;    
    EXIT WHEN NOT FOUND;
   
    update adm_kredit_angsuran
    set tanggal_jatuh_tempo=cast(CONCAT(DATE_PART('YEAR',tanggal_jatuh_tempo),'-',DATE_PART('MONTH',tanggal_jatuh_tempo),'-',htempo) as date), tanggal_mulai_tempo=cast(CONCAT(DATE_PART('YEAR',tanggal_mulai_tempo),'-',DATE_PART('MONTH',tanggal_mulai_tempo),'-',htempo) as date)
    where batasan_permohonan_kredit=idaspk and angsuran_ke > 1;


end loop ;  
CLOSE Cur1; 
END $$;

Looping dalam looping (2) - PostgreSQL

DO $$
    Declare idmaset int;   
      declare idkrtaset int;   
        Declare bulanke int;   
        DECLARE idasetinv int;   
        Declare jwl date;

    cur1 CURSOR for select ID from m_aset_inventaris
                                    where id_bpr = 2 and id_bpr_cabang = 1;   
   
    cur2 cursor for select id, depresiasi_bulan_ke, id_aset_inventaris, jwl_depresiasi
                                    from m_kartu_aset_inventaris where id_aset_inventaris=idmaset
                                    order by depresiasi_bulan_ke;
   
 begin 
OPEN Cur1 ; 
Loop    

    FETCH  next from Cur1 INTO idmaset;    
    EXIT WHEN NOT FOUND;
   
    open cur2;
        loop
       
        FETCH  next from Cur2 INTO idkrtaset, bulanke, idasetinv, jwl;    
    EXIT WHEN NOT FOUND;


    update m_kartu_aset_inventaris
    set jwl_depresiasi=((select jwl_depresiasi from m_kartu_aset_inventaris where depresiasi_bulan_ke < bulanke  and id_aset_inventaris=idasetinv order by depresiasi_bulan_ke desc limit 1) +(interval '1 MONTH'))
    where id_aset_inventaris=idasetinv and depresiasi_bulan_ke=bulanke and depresiasi_bulan_ke >1;
   
        END LOOP;
  CLOSE cur2;
   
end loop ;  
CLOSE Cur1; 
END $$;

Dynamic Query - SQL Server

Berikut adalah contoh dari bentuk dari Dynamic Query di SQL Server yang dibungkus ke dalam Store Procedure:  USE [Data00] GO SET ANSI_NULLS ...

Popular Posts