text: --=========================== DROP FK ===========================
  --==== KWT_BLOK ====
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_BLOK_KWT_SEKTOR') AND parent_object_id = OBJECT_ID(N'KWT_BLOK'))
  ALTER TABLE KWT_BLOK DROP CONSTRAINT FK_KWT_BLOK_KWT_SEKTOR
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_BLOK_KWT_CLUSTER') AND parent_object_id = OBJECT_ID(N'KWT_BLOK'))
  ALTER TABLE KWT_BLOK DROP CONSTRAINT FK_KWT_BLOK_KWT_CLUSTER
  
  --==== PKL ====
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_LOKASI_PKL_KWT_SK_SEWA') AND parent_object_id = OBJECT_ID(N'KWT_LOKASI_PKL'))
  ALTER TABLE KWT_LOKASI_PKL DROP CONSTRAINT FK_KWT_LOKASI_PKL_KWT_SK_SEWA
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_PKL_KWT_LOKASI_PKL') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_PKL'))
  ALTER TABLE KWT_TARIF_PKL DROP CONSTRAINT FK_KWT_TARIF_PKL_KWT_LOKASI_PKL
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_PKL_KWT_TIPE_PKL') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_PKL'))
  ALTER TABLE KWT_TARIF_PKL DROP CONSTRAINT FK_KWT_TARIF_PKL_KWT_TIPE_PKL
  GO
  --==== KSP ====
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_LOKASI_KSP_KWT_SK_SEWA') AND parent_object_id = OBJECT_ID(N'KWT_LOKASI_KSP'))
  ALTER TABLE KWT_LOKASI_KSP DROP CONSTRAINT FK_KWT_LOKASI_KSP_KWT_SK_SEWA
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_KSP_KWT_LOKASI_KSP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_KSP'))
  ALTER TABLE KWT_TARIF_KSP DROP CONSTRAINT FK_KWT_TARIF_KSP_KWT_LOKASI_KSP
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_KSP_KWT_TIPE_KSP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_KSP'))
  ALTER TABLE KWT_TARIF_KSP DROP CONSTRAINT FK_KWT_TARIF_KSP_KWT_TIPE_KSP
  GO
  
  --==== MP ====
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_MPD_KWT_LOKASI_MP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_MPD'))
  ALTER TABLE KWT_TARIF_MPD DROP CONSTRAINT FK_KWT_TARIF_MPD_KWT_LOKASI_MP
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_MPD_KWT_TARIF_MP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_MPD'))
  ALTER TABLE KWT_TARIF_MPD DROP CONSTRAINT FK_KWT_TARIF_MPD_KWT_TARIF_MP
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_MP_KWT_SK_SEWA') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_MP'))
  ALTER TABLE KWT_TARIF_MP DROP CONSTRAINT FK_KWT_TARIF_MP_KWT_SK_SEWA
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_MP_KWT_TIPE_MP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_MP'))
  ALTER TABLE KWT_TARIF_MP DROP CONSTRAINT FK_KWT_TARIF_MP_KWT_TIPE_MP
  GO
  
  --==== PSP ====
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_PSP_KWT_SK_PSP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_PSP'))
  ALTER TABLE KWT_TARIF_PSP DROP CONSTRAINT FK_KWT_TARIF_PSP_KWT_SK_PSP
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_PSP_KWT_TIPE_PSP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_PSP'))
  ALTER TABLE KWT_TARIF_PSP DROP CONSTRAINT FK_KWT_TARIF_PSP_KWT_TIPE_PSP
  GO
  
  IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'FK_KWT_TARIF_PSP_KWT_FUNGSI_PSP') AND parent_object_id = OBJECT_ID(N'KWT_TARIF_PSP'))
  ALTER TABLE KWT_TARIF_PSP DROP CONSTRAINT FK_KWT_TARIF_PSP_KWT_FUNGSI_PSP
  GO
  
  
  --=========================== CREATE FK ===========================
  --==== KWT_BLOK ====
  ALTER TABLE KWT_BLOK WITH CHECK ADD CONSTRAINT FK_KWT_BLOK_KWT_SEKTOR FOREIGN KEY(KODE_SEKTOR)
  	REFERENCES KWT_SEKTOR (KODE_SEKTOR)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_BLOK CHECK CONSTRAINT FK_KWT_BLOK_KWT_SEKTOR
  GO
  
  ALTER TABLE KWT_BLOK WITH CHECK ADD CONSTRAINT FK_KWT_BLOK_KWT_CLUSTER FOREIGN KEY(KODE_CLUSTER)
  	REFERENCES KWT_CLUSTER (KODE_CLUSTER)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_BLOK CHECK CONSTRAINT FK_KWT_BLOK_KWT_CLUSTER
  GO
  
  
  --==== PKL ====
  ALTER TABLE KWT_LOKASI_PKL WITH CHECK ADD CONSTRAINT FK_KWT_LOKASI_PKL_KWT_SK_SEWA FOREIGN KEY(KODE_SK)
  	REFERENCES KWT_SK_SEWA (KODE_SK)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_LOKASI_PKL CHECK CONSTRAINT FK_KWT_LOKASI_PKL_KWT_SK_SEWA
  GO
  
  ALTER TABLE KWT_TARIF_PKL WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_PKL_KWT_LOKASI_PKL FOREIGN KEY(KODE_LOKASI)
  	REFERENCES KWT_LOKASI_PKL (KODE_LOKASI)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_PKL CHECK CONSTRAINT FK_KWT_TARIF_PKL_KWT_LOKASI_PKL
  GO
  
  ALTER TABLE KWT_TARIF_PKL WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_PKL_KWT_TIPE_PKL FOREIGN KEY(KODE_TIPE)
  	REFERENCES KWT_TIPE_PKL (KODE_TIPE)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_PKL CHECK CONSTRAINT FK_KWT_TARIF_PKL_KWT_TIPE_PKL
  GO
  
  --==== KSP ====
  ALTER TABLE KWT_LOKASI_KSP WITH CHECK ADD CONSTRAINT FK_KWT_LOKASI_KSP_KWT_SK_SEWA FOREIGN KEY(KODE_SK)
  	REFERENCES KWT_SK_SEWA (KODE_SK)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_LOKASI_KSP CHECK CONSTRAINT FK_KWT_LOKASI_KSP_KWT_SK_SEWA
  GO
  
  ALTER TABLE KWT_TARIF_KSP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_KSP_KWT_LOKASI_KSP FOREIGN KEY(KODE_LOKASI)
  	REFERENCES KWT_LOKASI_KSP (KODE_LOKASI)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_KSP CHECK CONSTRAINT FK_KWT_TARIF_KSP_KWT_LOKASI_KSP
  GO
  
  ALTER TABLE KWT_TARIF_KSP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_KSP_KWT_TIPE_KSP FOREIGN KEY(KODE_TIPE)
  	REFERENCES KWT_TIPE_KSP (KODE_TIPE)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_KSP CHECK CONSTRAINT FK_KWT_TARIF_KSP_KWT_TIPE_KSP
  GO
  
  --==== MP ====
  ALTER TABLE KWT_TARIF_MPD WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_MPD_KWT_LOKASI_MP FOREIGN KEY(KODE_LOKASI)
  	REFERENCES KWT_LOKASI_MP (KODE_LOKASI)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_MPD CHECK CONSTRAINT FK_KWT_TARIF_MPD_KWT_LOKASI_MP
  GO
  
  ALTER TABLE KWT_TARIF_MPD WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_MPD_KWT_TARIF_MP FOREIGN KEY(KEY_MP)
  	REFERENCES KWT_TARIF_MP (KEY_MP)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_MPD CHECK CONSTRAINT FK_KWT_TARIF_MPD_KWT_TARIF_MP
  GO
  
  ALTER TABLE KWT_TARIF_MP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_MP_KWT_SK_SEWA FOREIGN KEY(KODE_SK)
  	REFERENCES KWT_SK_SEWA (KODE_SK)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_MP CHECK CONSTRAINT FK_KWT_TARIF_MP_KWT_SK_SEWA
  GO
  
  ALTER TABLE KWT_TARIF_MP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_MP_KWT_TIPE_MP FOREIGN KEY(KODE_TIPE)
  	REFERENCES KWT_TIPE_MP (KODE_TIPE)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_MP CHECK CONSTRAINT FK_KWT_TARIF_MP_KWT_TIPE_MP
  GO
  
  --==== PSP ====
  ALTER TABLE KWT_TARIF_PSP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_PSP_KWT_SK_PSP FOREIGN KEY(KODE_SK)
  	REFERENCES KWT_SK_PSP (KODE_SK)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_PSP CHECK CONSTRAINT FK_KWT_TARIF_PSP_KWT_SK_PSP
  GO
  
  ALTER TABLE KWT_TARIF_PSP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_PSP_KWT_TIPE_PSP FOREIGN KEY(KODE_TIPE)
  	REFERENCES KWT_TIPE_PSP (KODE_TIPE)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_PSP CHECK CONSTRAINT FK_KWT_TARIF_PSP_KWT_TIPE_PSP
  GO
  
  ALTER TABLE KWT_TARIF_PSP WITH CHECK ADD CONSTRAINT FK_KWT_TARIF_PSP_KWT_FUNGSI_PSP FOREIGN KEY(KODE_FUNGSI)
  	REFERENCES KWT_FUNGSI_PSP (KODE_FUNGSI)
  	ON UPDATE CASCADE
  	ON DELETE CASCADE
  GO
  ALTER TABLE KWT_TARIF_PSP CHECK CONSTRAINT FK_KWT_TARIF_PSP_KWT_FUNGSI_PSP
  GO
  
  --==================================== TRIGGER ====================================
  
  -- ========================= NOMOR PELANGGAN =========================
  --KWT_PELANGGAN (NO_PELANGGAN)
  IF OBJECT_ID ('KWT_PELANGGAN_NO_PELANGGAN', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_PELANGGAN_NO_PELANGGAN;
  GO
  CREATE TRIGGER KWT_PELANGGAN_NO_PELANGGAN
  	ON KWT_PELANGGAN
  	INSTEAD OF INSERT
  AS
  	SET NOCOUNT ON
  	DECLARE @np VARCHAR(15) = 
  	(
  		SELECT
  			ISNULL(KODE_PEL,'00') + 
  			RIGHT(YEAR(GETDATE()),2) +
  			'0' + -- BELUM TAU DARI MANA
  			(
  				SELECT 
  					RIGHT('0000000' + CAST((COU_NP + 1) AS VARCHAR), 7) 
  				FROM KWT_PARAMETER
  			)
  		FROM KWT_SEKTOR 
  		WHERE KODE_SEKTOR = (SELECT KODE_SEKTOR FROM INSERTED)
  	)
  	
  	INSERT INTO KWT_PELANGGAN (NO_PELANGGAN, TGL_PPJB, INFO_TAGIHAN, KODE_SEKTOR, KODE_CLUSTER, KODE_BLOK, LUAS_KAVLING, LUAS_BANGUNAN, STATUS_BLOK, NAMA_PELANGGAN, NO_KTP, NPWP, ALAMAT, NO_TELEPON, NO_HP, DEBET_BANK, KODE_BANK, NO_REKENING, AKTIF_AIR, KODE_ZONA, TIPE_AIR, KEY_AIR, TGL_PEMUTUSAN, PETUGAS, PERIODE_PUTUS, AKTIF_IPL, TIPE_IPL, KEY_IPL, KETERANGAN, STATUS_JOIN, JOIN_TO, USER_JOIN, JOIN_DATE, STATUS_SPLIT, SPLIT_FROM, USER_SPLIT, SPLIT_DATE, DISABLED) 
  	SELECT @np, TGL_PPJB, INFO_TAGIHAN, KODE_SEKTOR, KODE_CLUSTER, KODE_BLOK, LUAS_KAVLING, LUAS_BANGUNAN, STATUS_BLOK, NAMA_PELANGGAN, NO_KTP, NPWP, ALAMAT, NO_TELEPON, NO_HP, DEBET_BANK, KODE_BANK, NO_REKENING, AKTIF_AIR, KODE_ZONA, TIPE_AIR, KEY_AIR, TGL_PEMUTUSAN, PETUGAS, PERIODE_PUTUS, AKTIF_IPL, TIPE_IPL, KEY_IPL, KETERANGAN, STATUS_JOIN, JOIN_TO, USER_JOIN, JOIN_DATE, STATUS_SPLIT, SPLIT_FROM, USER_SPLIT, SPLIT_DATE, DISABLED FROM INSERTED
  	
  	UPDATE KWT_PARAMETER SET COU_NP = COU_NP + 1
  GO
  
  -- ========================= INVOICE =========================
  --KWT_PEMBAYARAN_AI (NO_INVOICE)
  IF OBJECT_ID ('KWT_PEMBAYARAN_AI_IVC', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_PEMBAYARAN_AI_IVC;
  GO
  CREATE TRIGGER KWT_PEMBAYARAN_AI_IVC
  	ON KWT_PEMBAYARAN_AI
  	AFTER INSERT
  AS
  	SET NOCOUNT ON
  	DECLARE 
  	@n VARCHAR(50),
  	@rkv VARCHAR(50), @rbg VARCHAR(50), @rdb VARCHAR(50), @rhn VARCHAR(50), @rrv VARCHAR(50), @rdr VARCHAR(50),
  	@idp VARCHAR(50), @trx VARCHAR(1),
  	@my VARCHAR(10) = 
  		CASE MONTH(GETDATE())
  			WHEN 1 THEN 'I' WHEN 2 THEN 'II' WHEN 3 THEN 'III' WHEN 4 THEN 'IV' WHEN 5 THEN 'V' WHEN 6 THEN 'VI' WHEN 7 THEN 'VII' WHEN 8 THEN 'VIII' WHEN 9 THEN 'IX' WHEN 10 THEN 'X' WHEN 11 THEN 'XI' WHEN 12 THEN 'XII'
  			ELSE '-'
  		END + '/' +
  		CAST(YEAR(GETDATE()) AS VARCHAR)
  		
  	SELECT TOP 1 
  		@rkv = REG_IVC_KV,
  		@rbg = REG_IVC_BG,
  		@rdb = REG_IVC_DB,
  		@rhn = REG_IVC_HN,
  		@rrv = REG_IVC_RV,
  		@rdr = REG_IVC_DR
  	FROM KWT_PARAMETER
  	
  	DECLARE cur_ins CURSOR FOR SELECT ID_PEMBAYARAN, TRX FROM INSERTED
  	OPEN cur_ins
  	
  	FETCH NEXT FROM cur_ins INTO @idp, @trx
  	
  	WHILE @@FETCH_STATUS = 0
  	BEGIN
  	
  		IF (@trx = '1') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_KV + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rkv + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_KV = COU_IVC_KV + 1
  		END
  		ELSE IF (@trx = '2') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_BG + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rbg + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_BG = COU_IVC_BG + 1
  		END
  		ELSE IF (@trx = '3') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_DB + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rdb + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_DB = COU_IVC_DB + 1
  		END
  		ELSE IF (@trx = '4') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_HN + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rhn + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_HN = COU_IVC_HN + 1
  		END
  		ELSE IF (@trx = '5') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_RV + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rrv + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_RV = COU_IVC_RV + 1
  		END
  		ELSE IF (@trx = '6') BEGIN
  			SELECT TOP 1 @n = CAST((COU_IVC_DR + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  			UPDATE KWT_PEMBAYARAN_AI SET NO_INVOICE = @n + @rdr + @my WHERE ID_PEMBAYARAN = @idp
  			UPDATE KWT_PARAMETER SET COU_IVC_DR = COU_IVC_DR + 1
  		END
  		
  		FETCH NEXT FROM cur_ins INTO @idp, @trx
  	END
  	
  	CLOSE cur_ins
  	DEALLOCATE cur_ins
  GO
  
  -- ========================= UPDATE KWITANSI =========================
  --KWT_PEMBAYARAN_AI (STATUS_BAYAR)
  IF OBJECT_ID ('KWT_PEMBAYARAN_AI_KWT', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_PEMBAYARAN_AI_KWT;
  GO
  CREATE TRIGGER KWT_PEMBAYARAN_AI_KWT 
  	ON KWT_PEMBAYARAN_AI
  	AFTER UPDATE
  AS 
  IF UPDATE (STATUS_BAYAR)
  BEGIN
  	SET NOCOUNT ON
  	DECLARE 
  	@n VARCHAR(50),
  	@rkv VARCHAR(50), @rbg VARCHAR(50), @rdb VARCHAR(50), @rhn VARCHAR(50), @rrv VARCHAR(50), @rdr VARCHAR(50),
  	@idp VARCHAR(50), @trx VARCHAR(1), @sb VARCHAR(10),
  	@my VARCHAR(10) = 
  		CASE MONTH(GETDATE())
  			WHEN 1 THEN 'I' WHEN 2 THEN 'II' WHEN 3 THEN 'III' WHEN 4 THEN 'IV' WHEN 5 THEN 'V' WHEN 6 THEN 'VI' WHEN 7 THEN 'VII' WHEN 8 THEN 'VIII' WHEN 9 THEN 'IX' WHEN 10 THEN 'X' WHEN 11 THEN 'XI' WHEN 12 THEN 'XII'
  			ELSE '-'
  		END + '/' +
  		CAST(YEAR(GETDATE()) AS VARCHAR)
  
  	SELECT TOP 1 
  		@rkv = REG_IVC_KV,
  		@rbg = REG_IVC_BG,
  		@rdb = REG_IVC_DB,
  		@rhn = REG_IVC_HN,
  		@rrv = REG_IVC_RV,
  		@rdr = REG_IVC_DR
  	FROM KWT_PARAMETER
  	
  	DECLARE cur_upd CURSOR FOR SELECT ID_PEMBAYARAN, TRX, STATUS_BAYAR FROM INSERTED;
  	OPEN cur_upd;
  	
  	FETCH NEXT FROM cur_upd INTO @idp, @trx, @sb
  	
  	WHILE @@FETCH_STATUS = 0
  	BEGIN
  	
  		IF (@sb = '2')
  		BEGIN
  			IF (@trx = '1') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_KV + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rkv + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_KV = COU_KWT_KV + 1
  			END
  			ELSE IF (@trx = '2') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_BG + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rbg + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_BG = COU_KWT_BG + 1
  			END
  			ELSE IF (@trx = '3') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_DB + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rdb + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_DB = COU_KWT_DB + 1
  			END
  			ELSE IF (@trx = '4') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_HN + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rhn + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_HN = COU_KWT_HN + 1
  			END
  			ELSE IF (@trx = '5') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_RV + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rrv + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_RV = COU_KWT_RV + 1
  			END
  			ELSE IF (@trx = '6') BEGIN
  				SELECT TOP 1 @n = CAST((COU_KWT_DR + 1) AS VARCHAR(10)) FROM KWT_PARAMETER
  				UPDATE KWT_PEMBAYARAN_AI SET NO_KWITANSI = @n + @rdr + @my WHERE ID_PEMBAYARAN = @idp
  				UPDATE KWT_PARAMETER SET COU_KWT_DR = COU_KWT_DR + 1
  			END
  		END
  		
  		FETCH NEXT FROM cur_upd INTO @idp, @trx, @sb
  	END
  	
  	CLOSE cur_upd
  	DEALLOCATE cur_upd
  END
  
  
  
  
  
  -- ================================== FASILITAS ==================================
  --KWT_LOKASI_PKL
  IF OBJECT_ID ('KWT_LOKASI_PKL_UPD_KODE_SK', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_LOKASI_PKL_UPD_KODE_SK;
  GO
  CREATE TRIGGER KWT_LOKASI_PKL_UPD_KODE_SK 
  	ON KWT_LOKASI_PKL
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_SK)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_LOKASI_PKL
  	SET
  		KODE_LOKASI = KODE_SK + '-' + SUBSTRING(KODE_LOKASI, CHARINDEX('-',KODE_LOKASI)+1, 3)
  	WHERE
  		KODE_SK IN (SELECT KODE_SK FROM INSERTED)
  END
  GO
  
  --KWT_LOKASI_KSP
  IF OBJECT_ID ('KWT_LOKASI_KSP_UPD_KODE_SK', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_LOKASI_KSP_UPD_KODE_SK;
  GO
  CREATE TRIGGER KWT_LOKASI_KSP_UPD_KODE_SK 
  	ON KWT_LOKASI_KSP
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_SK)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_LOKASI_KSP
  	SET
  		KODE_LOKASI = KODE_SK + '-' + SUBSTRING(KODE_LOKASI, CHARINDEX('-',KODE_LOKASI)+1, 3)
  	WHERE
  		KODE_SK IN (SELECT KODE_SK FROM INSERTED)
  END
  GO
  
  --KWT_TARIF_PKL
  IF OBJECT_ID ('KWT_TARIF_PKL_UPD_KEY', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_TARIF_PKL_UPD_KEY;
  GO
  CREATE TRIGGER KWT_TARIF_PKL_UPD_KEY 
  	ON KWT_TARIF_PKL
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_LOKASI) OR UPDATE (KODE_TIPE)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_TARIF_PKL
  	SET
  		KEY_PKL = KODE_LOKASI + '-' + KODE_TIPE
  	WHERE
  		KODE_LOKASI IN (SELECT KODE_LOKASI FROM INSERTED) OR
  		KODE_TIPE IN (SELECT KODE_TIPE FROM INSERTED)
  END
  GO
  
  --KWT_TARIF_KSP
  IF OBJECT_ID ('KWT_TARIF_KSP_UPD_KEY', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_TARIF_KSP_UPD_KEY;
  GO
  CREATE TRIGGER KWT_TARIF_KSP_UPD_KEY 
  	ON KWT_TARIF_KSP
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_LOKASI) OR UPDATE (KODE_TIPE)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_TARIF_KSP
  	SET
  		KEY_KSP = KODE_LOKASI + '-' + KODE_TIPE
  	WHERE
  		KODE_LOKASI IN (SELECT KODE_LOKASI FROM INSERTED) OR
  		KODE_TIPE IN (SELECT KODE_TIPE FROM INSERTED)
  END
  GO
  
  --KWT_TARIF_MP
  IF OBJECT_ID ('KWT_TARIF_MP_UPD_KEY', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_TARIF_MP_UPD_KEY;
  GO
  CREATE TRIGGER KWT_TARIF_MP_UPD_KEY 
  	ON KWT_TARIF_MP
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_SK) OR UPDATE (KODE_TIPE)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_TARIF_MP
  	SET
  		KEY_MP = KODE_SK + '-' + KODE_TIPE
  	WHERE
  		KODE_SK IN (SELECT KODE_SK FROM INSERTED) OR
  		KODE_TIPE IN (SELECT KODE_TIPE FROM INSERTED)
  END
  GO
  
  --KWT_TARIF_MPD
  IF OBJECT_ID ('KWT_TARIF_MPD_UPD_KEY', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_TARIF_MPD_UPD_KEY;
  GO
  CREATE TRIGGER KWT_TARIF_MPD_UPD_KEY 
  	ON KWT_TARIF_MPD
  	AFTER UPDATE
  AS 
  IF UPDATE (KEY_MP) OR UPDATE (KODE_LOKASI)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_TARIF_MPD
  	SET
  		KEY_MPD = KEY_MP + '-' + KODE_LOKASI
  	WHERE
  		KEY_MP IN (SELECT KEY_MP FROM INSERTED) OR
  		KODE_LOKASI IN (SELECT KODE_LOKASI FROM INSERTED)
  END
  GO
  
  --KWT_TARIF_PSP
  IF OBJECT_ID ('KWT_TARIF_PSP_UPD_KEY', 'TR') IS NOT NULL
  	DROP TRIGGER KWT_TARIF_PSP_UPD_KEY;
  GO
  CREATE TRIGGER KWT_TARIF_PSP_UPD_KEY 
  	ON KWT_TARIF_PSP
  	AFTER UPDATE
  AS 
  IF UPDATE (KODE_SK) OR UPDATE (KODE_TIPE) OR UPDATE (KODE_FUNGSI)
  BEGIN
  	SET NOCOUNT ON
  	UPDATE KWT_TARIF_PSP
  	SET
  		KEY_PSP = KODE_SK + '-' + KODE_TIPE + '-' + KODE_FUNGSI
  	WHERE
  		KODE_SK IN (SELECT KODE_SK FROM INSERTED) OR
  		KODE_TIPE IN (SELECT KODE_TIPE FROM INSERTED) OR
  		KODE_FUNGSI IN (SELECT KODE_FUNGSI FROM INSERTED)
  END
  GO
  
  