Posts

Showing posts with the label DB2

IBMi (AS400) fans only ' Journaling database physical files (tables)

Image
Journaling database physical files is a powerful, built-in OS feature. It allows you to audit data file changes. Tracks additions, updates, deletions of a physical file. Let's see how it works. Scenario: I have a phisical file (Tabel) called CLANA00F, I wont tracks additions, updates and deletions about CLANA00F. Step 1: CRTJRNRCV create a journal receiver in your library CRTJRNRCV JRNRCV(VDOTEST1/JRCV) THRESHOLD(1500000) TEXT('My Journal Receiver') Step 2: CRTJRN create a journal into the journal receiver CRTJRN JRN(VDOTEST1/JRNL) JRNRCV(VDOTEST1/JRCV) Here is the just created journal receiver and the journal.  Step 3: STRJRNPF start auditing STRJRNPF FILE(VDOTEST1/CLANA00F) JRN(VDOTEST1/JRNL) IMAGES(*BOTH) OMTJRNE(*OPNCLO) // the option OMTJRNE(*OPNCLO) omit open and close entries. // Open and close operations on the specified file members do not create open and close journal entries. It saves some storage space in the attached receivers.                                

IBMi (AS400) fans only ‘ how to read a flat file with sql embedded

Image
#IBMiSample It often happens that you have to read all the records of a file. Years ago I would have used the RPG cycle defining the Input/Primary file. Today, with the introduction of embedded SQL, I would do this: First of all I create a file, then I manually write some records. to create a file I write this SQL source: IPFLSQL.SQL -- --  RUNSQLSTM SRCFILE(myLib/MySouceFile) SRCMBR(IPFLSQL) -- --  Generazione tabella CREATE OR REPLACE TABLE myLib /IPFLI00F ( IIFANN CHARACTER(1) NOT NULL WITH DEFAULT, IITEXT CHARACTER(25) NOT NULL WITH DEFAULT ) RCDFMT IPFLI ;  Run IPFLSQL.SQL with RUNSQLSTM SRCFILE(myLib/MySouceFile) SRCMBR(IPFLSQL) to create the file IPFLI00F. Then populate IPFLI00F with some records. No matter what you type, just a few records are enough. Then compile and run  IPFL01.SQLRPGLE       **free       *******************************************************       *  How to read an entire file from the beginning      *       ************************************************

IBMi (AS400) fans only ' A simple way to create table with Rpg Free & SQL embedded

Image
#IBMiSample Compile and Run this SQLRPGLE to create table CLANA00F, index CLANA01L and table OTORD00F, and populate:       **free       *****************************       * create CLANA00F & OTORD00F       *****************************        ctl-opt option(*nodebugio) dftactgrp(*no) actgrp(*new);         // The EXEC SQL is never executed. It is used at compile time.         exec sql Set Option Commit = *None;         // Create File         exec sql           CREATE or REPLACE TABLE myLib/CLANA00F (           CLANN0 CHARACTER(01) NOT NULL WITH DEFAULT,           CLCCL0 NUMERIC(7, 0) GENERATED ALWAYS AS IDENTITY PRIMARY KEY,           CLCIB0 CHARACTER(03) NOT NULL WITH DEFAULT,           CLNOM0 CHARACTER(50) NOT NULL WITH DEFAULT           )           RCDFMT CLANA           ;         // Create index         exec sql           CREATE INDEX myLib/CLANA01L ON myLib/CLANA00F (CLCCL0 ASC);         // Fill values         exec sql           INSERT INTO CLANA00F (CLCIB0, CLNOM0)          

IBMi (AS400) fans only ' UTILITIES - How to retrieve physical file description

Image
                               

IBMi (AS400) fans only ' Encrypting/Decrypting data with RpgFree & SQL embedded

Image
  #IBMiSample Compile and Run this SQLRPGLE. This is an explanation how to encrypt/decrypt data in a DB2 table using RpgFree & SQL embedded.       **free       ************************************************       * Encrypting/Decrypting data with SQL embedded *       * Encrypt/decript example with                 *       * ENCRYPT_AES                                  *       * ENCRYPT_RC2                                  *       * ENCRYPT_TDES                                 *       *                                              *       * DECRYPT_BIT                                  *       ************************************************        ctl-opt        option(*nodebugio) dftactgrp(*no) actgrp(*new);        dcl-s encryptionPassword VARCHAR(128);        dcl-s p0                     CHAR(10);        dcl-s p1                     CHAR(10);        dcl-s p2                     CHAR(10);        dcl-s p3                     CHAR(10);        // The EXEC SQL is never executed. It is

IBMi (AS400) fans only ‘ Rpg Free: Create a Window to display a very long program message

Image
#IBMiSample I'm tired of one-line messages. Too short. The average user needs clear and comprehensive messages. Then I need a large enough window to display a clear and complete message . Why not use a SUBFILE? A SUBFILE program can handle a list of many lines, a clear and complete message is just a list of many text lines! Here's how I did it I need a table that contains the messages.                           --                                                  --  RUNSQLSTM SRCFILE(VDOTEST1/QSOURCE) SRCMBR(W03AM00F) --                                                  CREATE TABLE VDOTEST1/W03AM00F (                    W3FANN CHARACTER(1) NOT NULL WITH DEFAULT,          W3TPMS CHARACTER(6) NOT NULL WITH DEFAULT,          W3COD0 NUMERIC(4, 0) NOT NULL WITH DEFAULT,         W3RIG0 NUMERIC(2, 0) NOT NULL WITH DEFAULT,         W3TXT0 CHARACTER(078) NOT NULL WITH DEFAULT         )                                                   RCDFMT W03AM                                       

IBMi (AS400) fans only ' RUNSQLSTM Run SQL Scripts example

Image
#IBMiSample The Run SQL Statements (RUNSQLSTM) command processes a source file of Structure Query Language (SQL) statements. let's have an example: Create a source member AS52A00F into library VDOTEST1/QSQLSRC. VDOTEST1 is MyLibrary. Use your library name. AS52A00F.SQL

IBMi (AS400) fans only ' How to import a CSV file into db2

Image
#IBMiSample Let's try with a very simple csv like this, called file1.csv: Code;Name 3741;Joe 3742;Barth 3810; 3945; First of all we need to save file1.csv into ISF . In my PUB400 (free AS400 at pub400.com) I have this directory: /home/PALDO I copy file1.csv to  /home/PALDO Use standard FTP transfer software (I use WinScp, I think Filezilla will work too) Let's take a look inside my directory I type WRKLNK and I verify the existence of File1.csv It looks as expected: To import this file I need a similar table on DB2. In my example it means that I will create a table with a decimal field and a charachter field . I create the table TEMPCSV (PALDO1 is my Library): CREATE TABLE PALDO1.TEMPCSV ( CLCCL0 DECIMAL(7, 0) DEFAULT 0 , CLNOM0 CHAR(50) DEFAULT ' ' ) RCDFMT FMTXX Here's my new table TEMPCSV : The table  TEMPCSV  is now created and it is empty: We can optionally set up a source file to note lines not imported by the CPYF

IBMi (AS400) fans only Table with self-incrementing primary key and current time field

Image
#IBMiSample How to get a primary key field that increments itself and a time field that updates itself? Look at  LGID00 and  LGDTTM fields. Create a table: CREATE TABLE LGCUS00F ( LGID00 INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, LGDTTM TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, LGNAME CHAR(50), LGSURN CHAR(50)) RCDFMT LGCUS Now try to add a record: INSERT INTO LGCUS00F (LGNAME, LGSURN) VALUES ('Aldo', 'Succi') Let's see the result: SELECT * FROM LGCUS00F As you can see, LGID00=1 and LGDTTM=2018-04-01-10.01.49.781922 That's it! I appreciate all the comments made on this blog.

IBMi (AS400) fans only how does it works "ADD VERSIONING" and "USE HISTORY TABLE" to take trace of every changes on a table. This looks like magic!

Image
#IBMiSample This looks like magic! The operating system, in a totally automatic way, keeps track of every change in the data. This simple technique allows you to find the value of a field on a certain date and time. use STRSQL Step 1) CREATE TABLE CUSTOMER (CUSTOMER_ID BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH +1 INCREMENT BY +1 ) ,CUSTOMER_NAME VARCHAR(10) ,ADDRESS VARCHAR(20)) Step 2) alter table customer add system_start timestamp(12) generated always as row begin not null add system_end timestamp(12) generated always as row end not null add tx_start timestamp(12) generated always as transaction start id implicitly hidden add period system_time (system_start, system_end) Step 3) create table customer_h like customer Step 4) alter table customer add versioning use history table customer_h Step 5) Now use command line: CRTJRNRCV JRNRCV(VDOTEST1/JRNRCVNAME) THRESHOLD(150) TEXT('Journal receiver') Step 6) CRTJRN JRN(VDOTEST1/jrnname) JRNRCV(VDOTEST1/jrnrcv