Last active
February 21, 2017 02:53
-
-
Save phpdave/d7422a7381b320debc2e80fc3d880000 to your computer and use it in GitHub Desktop.
Playing with a temporal table on PUB400 on the IBM i DB2 for i https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_73/sqlp/rbafycrttemporaltable.htm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Creates the temporal table | |
CREATE OR REPLACE TABLE PHP_DAVE1.DEPT | |
(DEPTNO CHAR(3) NOT NULL, | |
DEPTNAME VARCHAR(36) NOT NULL, | |
MGRNO CHAR(6), | |
ADMRDEPT CHAR(3) NOT NULL, | |
LOCATION CHAR(16), | |
START_TS TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW BEGIN, | |
END_TS TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW END, | |
TS_ID TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS TRANSACTION START ID, | |
PERIOD SYSTEM_TIME (START_TS, END_TS), | |
PRIMARY KEY (DEPTNO)); | |
--Creates the history table | |
CREATE TABLE PHP_DAVE1.DEPTHIST LIKE PHP_DAVE1.DEPT; | |
--Links the main table to the history table | |
ALTER TABLE PHP_DAVE1.DEPT ADD VERSIONING USE HISTORY TABLE PHP_DAVE1.DEPTHIST; | |
--Creates a test record | |
INSERT INTO PHP_DAVE1.DEPT (DEPTNO, DEPTNAME, MGRNO, ADMRDEPT, LOCATION) VALUES ('2', 'Call Center Dept', '2', '2', '2'); | |
--Must have a journal on the temporal table to do updates and deletes | |
Call qcmdexc('CRTJRNRCV JRNRCV(PHP_DAVE1/DEPTJR) ASP(1) THRESHOLD(100000) AUT(*ALL) TEXT(''Test'')'); | |
Call qcmdexc('CRTJRN JRN(PHP_DAVE1/DEPTJ) JRNRCV(PHP_DAVE1/DEPTJR) ASP(1)'); | |
Call qcmdexc('STRJRNPF FILE(PHP_DAVE1/DEPT) JRN(PHP_DAVE1/DEPTJ) IMAGES(*BOTH)'); | |
Call qcmdexc('CRTJRNRCV JRNRCV(PHP_DAVE1/DEPTHISTJR) ASP(1) THRESHOLD(100000) AUT(*ALL) TEXT(''Test'')'); | |
Call qcmdexc('CRTJRN JRN(PHP_DAVE1/DEPTHISTJ) JRNRCV(PHP_DAVE1/DEPTHISTJR) ASP(1)'); | |
Call qcmdexc('STRJRNPF FILE(PHP_DAVE1/DEPTHIST) JRN(PHP_DAVE1/DEPTHISTJ) IMAGES(*BOTH)'); | |
--Modifies the record to show how the temporal table works by taking making a copy of the old record into the history table | |
UPDATE PHP_DAVE1.DEPT SET DEPTNAME='Customer Service' WHERE DEPTNO=2; | |
--View the records in the main table should see dept name Customer Service | |
SELECT * FROM PHP_DAVE1.DEPT; | |
--View the records in the history table should see dept name Call Center Dept | |
SELECT * FROM PHP_DAVE1.DEPTHIST; | |
-- SELECT * FROM PHP_DAVE1.DEPT FOR SYSTEM_TIME AS OF CURRENT TIMESTAMP - 4 minutes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment