Senin, 14 September 2020

Function Yang Outputnya Seperti Tabel - PostgreSQL

 CREATE OR REPLACE FUNCTION "public"."nominatif_deposito"("bpr" int4, "cab" int4, "startdate" date, "enddate" date, "tipe" int4)
  RETURNS TABLE("noreg" text, "norek" text, "tanggal_aplikasi" date, "nama_nasabah" text, "alamat" text, "kodeproduk" text, "suku_bunga" numeric, "carabayar" text, "jatuh_tempo" date, "saldoawal" numeric, "bunga" numeric, "pajak" numeric, "saldoakhir" numeric, "debet" numeric, "kredit" numeric, "ao" text, "namaproduk" text) AS $BODY$

Select Nama Kolom
 from 
Ket: Bagian ini adalah query nya,

$BODY$
  LANGUAGE sql VOLATILE
  COST 100

----------------------
select * from nominatif_deposito(4,15,'2020-08-01','2020-08-31',1);

Senin, 07 September 2020

Looping Untuk Menampilkan Tanggal 1 Sampai Dengan Tanggal AKhir Bulan - PostgreSQL

OUTPUT
userid      tanggal            tglaja
12345 2020-08-01 1
12345 2020-08-02 2
12345 2020-08-03 3
12345 2020-08-04 4
12345 2020-08-05 5
12345 2020-08-06 6
12345 2020-08-07 7
12345 2020-08-08 8
12345 2020-08-09 9
12345 2020-08-10 10
12345 2020-08-11 11
12345 2020-08-12 12
12345 2020-08-13 13
12345 2020-08-14 14
12345 2020-08-15 15
12345 2020-08-16 16
12345 2020-08-17 17
12345 2020-08-18 18
12345 2020-08-19 19
12345 2020-08-20 20
12345 2020-08-21 21
12345 2020-08-22 22
12345 2020-08-23 23
12345 2020-08-24 24
12345 2020-08-25 25
12345 2020-08-26 26
12345 2020-08-27 27
12345 2020-08-28 28
12345 2020-08-29 29
12345 2020-08-30 30
12345 2020-08-31 31

CARANYA:
DO $$
DECLARE
x date;
y integer;
awaltgl date;
akhirtgl date;

begin
delete from "temporary".tmp_list_tanggal_com where userid = 12345;
delete from "temporary".tmp_range_tanggal_com where userid = 12345;

insert into "temporary".tmp_range_tanggal_com 
select 
12345 userid,
( ( date_trunc( 'MONTH', CAST ( concat ( '2020-', '08-', '01' ) AS DATE ) ) ) :: DATE ) tglawal, 
((date_trunc( 'MONTH', CAST ( concat ( '2020-', '08-', '01' ) AS DATE ) ) + INTERVAL '1 MONTH - 1 day') :: DATE ) tglakhir;

select tglawal into awaltgl from "temporary".tmp_range_tanggal_com where userid = 12345; 
select tglakhir into akhirtgl from "temporary".tmp_range_tanggal_com where userid = 12345; 
x :=awaltgl;
y :=1;

loop
insert into "temporary".tmp_list_tanggal_com
VALUES (12345, x, y);
x :=(x + INTERVAL '1 day') :: date;
y := y+1;
exit when x > akhirtgl;
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