Friday 13 January 2017

Memeriksa Apakah String itu Sebuah Tanggal yang Valid atau Tidak

Kita dapat menggunakan fungsi berikut :

CREATE OR REPLACE FUNCTION IS_DATE (text) RETURNS  INTEGER AS $$
BEGIN
     IF ($1 IS NULL) THEN
         RETURN 0;
     END IF;
     PERFORM $1::date;
     RETURN 1;
EXCEPTION WHEN others THEN
     RETURN 0;
END;
$$ language plpgsql;

Referensi

Friday 6 January 2017

PL/SQL : Otomatis Mengupdate Nilai Suatu Kolom Berdasarkan Suatu Perubahan

Status : Draft

Mengupdate nilai suatu kolom, jika nilai kolom lain berubah

Contoh :
CREATE OR REPLACE FUNCTION soal_set_status_akhir()
RETURNS TRIGGER
AS $$
BEGIN
  NEW.status_akhir := CASE WHEN NEW.id_status_soal = 1 THEN 1
                      ELSE 0 END;
  RETURN NEW;
END $$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS soal_set_status_akhir ON soal;
CREATE TRIGGER soal_set_status_akhir
BEFORE INSERT OR UPDATE ON soal
FOR EACH ROW
  EXECUTE PROCEDURE soal_set_status_akhir();

Referensi

  1. Create Trigger, https://www.postgresql.org/docs/9.1/static/sql-createtrigger.html
  2. Postgresql Trigger based on value of one column to change or update value of other column in each row., http://stackoverflow.com/questions/20958569/postgresql-trigger-based-on-value-of-one-column-to-change-or-update-value-of-oth
  3. Dokumentasi : Database, http://banksoal.openthinklabs.com/2017/01/dokumentasi-database.html