Selasa, 10 Desember 2019

Looping Insert Tanggal Urut atau Bertambah Tiap bulannya - PostgreSQL

Contoh kita ingin update suatu tanggal sesuai dengan contoh tanggal di bawah ini:


--update jwl_deperesiasi
DO $$
    Declare idkrt int;   
        Declare bulanke int;   
        DECLARE idaset int;   
        Declare jwl date;

    cur1 CURSOR for select id, depresiasi_bulan_ke, id_aset_inventaris, jwl_depresiasi from m_kartu_depresiasi where id_aset_inventaris=1530 order by depresiasi_bulan_ke;   
 begin 
OPEN Cur1 ; 
Loop     
    FETCH  next from Cur1 INTO idkrt, bulanke, idaset, jwl;    
    EXIT WHEN NOT FOUND;
    update m_kartu_depresiasi
    set jwl_depresiasi=((select jwl_depresiasi from m_kartu_depresiasi where depresiasi_bulan_ke < bulanke  and id_aset_inventaris=idaset order by depresiasi_bulan_ke desc limit 1) + (interval '1 MONTH'))
    where id_aset_inventaris=idaset and depresiasi_bulan_ke=bulanke and depresiasi_bulan_ke >1;
end loop ;  
CLOSE Cur1; 
END $$;

Sabtu, 07 Desember 2019

Import dan Restore database - PostgreSQL

A. Restore /Import versi windows

PostgreSQL SECARA LOCAL
1. C -> Program Files (x86) -> PostgreSQL -> 9.6 -> bin -> psql -U postgres

2. lalu masukkan password


SECARA GLOBAL/SERVER LAIN

1. C -> Program Files (x86) -> PostgreSQL -> 9.6 -> bin -> psql -U postgres -115.75.333.324
2. postgres=# \c bank_aty

(bpr_aty) nama database target

3. bank_aty=# \i C:/backup_data__20170619141626.backup

folder atau file jangan pakai spasi

B. Restore /Import versi LINUX (Ubuntu 16.04)

cara restore pake backup
pg_restore -i -h 10.5.2.30 -p 5432 -U postgres -d data_bank -v "C:/database_kosong.backup"

atau
pg_restore -h 10.5.2.30 -p 5432 -U postgres -d data_bank -v "C:/database_kosong.backup"

jika ada error seperti "FATAL: password authentication failed for user "postgres"
maka di pg_hba.conf ubah seperti ini:
local postgres postgres peer

lalu ALTER USER postgres PASSWORD 'my_postgres_password';

Sumber: https://hariatysimangunsong.blogspot.com/2019/06/cara-import-dan-restore-data-dum.html

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 $$;

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