Run SQL on IBM i from a 5250 session: STRSQL, RUNSQL and RUNSQLSTM
STRSQL opens interactive SQL on the green screen. RUNSQL runs one statement from a command line or CL. RUNSQLSTM runs a script from a source member or IFS file.
IBM i has three CL commands for SQL in a 5250 session. Pick one by how much SQL you have and whether you want to see results.
STRSQL: type and run statements
STRSQL
opens the Enter SQL Statements display. Type a statement, press Enter, and the result of a SELECT is shown on the screen. F4 prompts, F16, F17 and F18 list libraries and objects to pick from, and messages appear on the same display.
Two parameters matter most:
NAMING:*SYS(the default) useslibrary/filenames,*SQLusesschema.table. Start withSTRSQL NAMING(*SQL)if you are used to the SQL style.COMMIT: the default*NONEruns without commitment control, so changes take effect at once.
RUNSQL: one statement from a command line or CL
RUNSQL SQL('insert into testlib/t1 values (1)')
- One statement, up to 5000 bytes. Double any quote inside the statement:
''TESTLIB''. - It does not produce a spooled file or show rows. IBM's example for a query saves the result in a table, for example
CREATE TABLE QTEMP.T1 AS (SELECT ...) WITH DATA. - It does not commit for you. The default is
COMMIT(*CHG), so changes run under commitment control and the commit is up to your program. NAMINGdefaults to*SYS, as in STRSQL. AddNAMING(*SQL)to writeschema.table.
It works well inside CL programs, where the statement can be built from variables.
RUNSQLSTM: run a script
Put the statements in a source member or an IFS file, then:
RUNSQLSTM SRCFILE(MYLIB/QSQLSRC) SRCMBR(MYSCRIPT)
RUNSQLSTM SRCSTMF('/home/user1/script.sql')
RUNSQLSTM writes a listing to a spooled file, so errors can be read afterwards. ERRLVL sets how severe an error must be to stop the run (default 10). Like RUNSQL, it defaults to COMMIT(*CHG), but it commits or rolls back by itself when it ends.
SQL7008 reason code 3: not journaled
SQL7008 ... not valid for operation. The reason code is 3.
Reason code 3 means the file is not journaled, or you have no authority to its journal. Commitment control needs a journal, and RUNSQL and RUNSQLSTM use commitment control by default. Either journal the file, or run without commitment control:
RUNSQL SQL('insert into testlib/t1 values (1)') COMMIT(*NONE)
Related
- IBM i CL basics
- Automate IBM i green-screen tasks: job scheduler, SBMJOB, SSH and screen scripts
- IBM i shells: QSH, QP2TERM or SSH, and which to use
- IBM i job log missing or empty: CHGJOB LOG, LOGCLPGM and pending job logs
- IBM i job in MSGW status: find the message and reply to it