Sabtu, 07 Desember 2019

Looping Sampai Angka Tertentu - PostgreSQL

DO $$
DECLARE
    x INTEGER;
begin
x :=1;
loop
    insert into zz_test_insert VALUES (x);
    x := x + 1;
    exit when x > 101;
end loop;
end $$;

----
DO $$
DECLARE
    x INTEGER;
begin
x :=1;
FOR x IN 1..100 LOOP
    insert into zz_test_insert VALUES (x);
    x := x + 1;  
end loop;
end $$;

-------------------------------------
DO $$
        DECLARE idkartu INTEGER;
        DECLARE bulan_ke INTEGER;
    Declare harga bigint;    
        Declare jangka int;    
        Declare tglbeli date;
        declare aset record;
 begin
idkartu :=(select COALESCE(max(id),0) idmax from m_kartu_depresiasi) + 1;
 bulan_ke :=1;

loop
  
    select jangka_waktu, id into aset from m_aset_inventaris where id=1592;  
  
    insert into m_kartu_depresiasi
    (id, depresiasi_bulan_ke, id_aset_inventaris,status_depresiasi)    values    (idkartu, bulan_ke, aset.id, false);
  
    idkartu := idkartu + 1;
    bulan_ke := bulan_ke + 1;
    exit when bulan_ke > aset.jangka_waktu;

end loop ;
END $$;

Tidak ada komentar:

Posting Komentar

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