Quantcast
Channel: I Think - Therefore "IBM i" - #SQLRPGLE
Viewing all 13 articles
Browse latest View live

SQLRPGLE getting a count with variable file name

$
0
0

Been having fun with SQL and RPGLE this afternoon... the question was "how do I find out if and how many Policy numbers exist in one of the Policy files at any given time?" So, this meant using a variable file name in the RPG code, with a variable policy number. Took some playing around but found a solution that worked great and it looks like this:

A Dynamic SQL statement with a variable file name in RPGLE

/free

wSelectStatmnt = 'Select Count(*) '
 + 'From ' + %Trim(wInpFileName)
 + cBlank
 + ' Where LsPol# = '
 + cSqlQuote
 + %Trim(IwPol#)
 + cSqlQuote;

exec sql prepare stmt from :wSelectStatmnt ;

exec sql declare C1 cursor with return to client for stmt;

exec sql open C1;

exec sql fetch C1 into :wNumClaims ;

Dsply wNumClaims;  

exec sql Close C1;

/end-free

SQL Joins in the world of IBM i - like Join Logical Files but sexier

$
0
0
learning SQL is a balancing act

In my ongoing mission to use SQL rather than the older style direct file IO (which I will called DDS) - I've found lots of improvements and things that I really like about SQL in general. One of the confusions for me is remembering the SQL'style of describing its functions.

In DDS its a file in SQL its a TABLE

In DDS its Record and Field in SQL its a ROW and COLUMN

Thats the easy but but remember the various ways you can join two files together (called a JOIN LOGICAL FILE in DDS) but SQL offers us some rich and easy ways of joining everything, partial matches or even things that dont match. Here is a snippet of a great article from the coding horror website:

A Visual Explanation of SQL Joins

SQLRPGLE how to update a file with a cursor - WHERE CURRENT OF

$
0
0
so what does WHERE CURRENT OF mean?

So what does WHERE CURRENT OF mean?

Quite simply it means - if we have just read something using an SQL cursor then the operation that we are applying to "where current of" is being applied to the exact data set that just been read.

Let's take an example of direct file access technique for reading a specific record and updating it. How about we read every record in a file with the value of 'X' in s status field and change the value to 'Y'

Read filename;

Dow not %EOF(filename);

If Status = 'X';

   // some program logic stuff could be here

  Status = 'Y';

Endif;

Read filename;

Enddo;

note: this could just as easily be a DELETE operation.

If we wanted to do the same thing in SQL we could do it like this:

exec sql declare NicksCursor cursor for

         select * from filename

         where status = 'X'

         for update of Status;

exec sql open NicksCursor;

What is the difference between /COPYand /INCLUDE in SQL RPGLE

$
0
0

Using COPYBOOKS in the RPG programming language

/copy and /include both are used to add program code to our RPGLE and SQLRPGLE programs. Both commands will suck in program source code from a copybook - typically stored in QCPYLESRC.

There is one onteable difference between how the compile directives work in RPGLE and SQLRPGLE programs : in RPG ILE programs the /copy directive is expanded by sql preprocessior, but /include does not get expanded by SQLRPGLE code.

Why? The /COPY and /INCLUDE directives have the same purpose and the same syntax, but are handled differently by the SQL precompiler. If your program does not have embedded SQL, you can freely choose which directive to use. If your program has embedded SQL then stick with /copy but if it's a native RPGLE program then you can use either.

What is a Copybook?

/copy and /include copybook code is "pulled" into the source by the compiler before executing the compile. this eliminates having to duplicate the code a million times over (but I mainly use for prototypes, data structures...not calculations!).

 

 

/define is used to segregate code in copybooks (or skip code depending on usage).

Get the IBM i System Name using RPGLE or SQLRPGLE

$
0
0
system names and nicknames are scary in big dark computer rooms

Get Sysname using SQL

I just found a new way of grabbing the IBM i System Name from within an RPG program. using SQL is neat and oh-so-simple!

exec sql values current server into :systemName;
If systemName = 'my-ibm-i-system-name';
  do some stuff...
endif;

 

Get Sysname using CL

The good old tried and tested technique is to write a simple Control Language Program that looks something like this:

dcl &SysName *char
rtvneta sysname( &SysName )

the main drawback with this simple method is that you have another program out there to be called by any/all programs that want to find the system name.

 

Get Sysname using IBM API

Most RPG programmers would probably choose to call the IBM *API to Retrieve Network Attributes (QWCRNETA). This has the same affect as calling a CL to retrieve the network attributes, but of course you could also call the *API from the CLP program if you wanted to be uber-tricky ;)

Modernize RPG code for 'Get the System Name' for IBM i, iSeries and AS400

$
0
0
running free like RPG in big dark computer rooms

For any RPG programmer, writing RPGLE code (sometimes called RPG4) is fun. But, upgrading older RPG progams to the latest free form program layout is even more fun! So lets look at any example:

SQLRPGLE getting a count with variable file name

$
0
0

Been having fun with SQL and RPGLE this afternoon... the question was "how do I find out if and how many Policy numbers exist in one of the Policy files at any given time?" So, this meant using a variable file name in the RPG code, with a variable policy number. Took some playing around but found a solution that worked great and it looks like this:

A Dynamic SQL statement with a variable file name in RPGLE

/free

wSelectStatmnt = 'Select Count(*) '
 + 'From ' + %Trim(wInpFileName)
 + cBlank
 + ' Where LsPol# = '
 + cSqlQuote
 + %Trim(IwPol#)
 + cSqlQuote;

exec sql prepare stmt from :wSelectStatmnt ;

exec sql declare C1 cursor with return to client for stmt;

exec sql open C1;

exec sql fetch C1 into :wNumClaims ;

Dsply wNumClaims;

exec sql Close C1;

/end-free

SQL Joins in the world of IBM i - like Join Logical Files but sexier

$
0
0
learning SQL is a balancing act

In my ongoing mission to use SQL rather than the older style direct file IO (which I will called DDS) - I've found lots of improvements and things that I really like about SQL in general. One of the confusions for me is remembering the SQL'style of describing its functions.

In DDS its a file in SQL its a TABLE

In DDS its Record and Field in SQL its a ROW and COLUMN

Thats the easy but but remember the various ways you can join two files together (called a JOIN LOGICAL FILE in DDS) but SQL offers us some rich and easy ways of joining everything, partial matches or even things that dont match. Here is a snippet of a great article from the coding horror website:

A Visual Explanation of SQL Joins


What is the difference between /COPY and /INCLUDE in SQL RPGLE

$
0
0

Using COPYBOOKS in the RPG programming language

/copy and /include both are used to add program source code to RPGLE and SQLRPGLE programs.

Both commands will suck in program source code from a copybook - typically stored in QCPYLESRC - But there is one big difference between how the compile directives work in RPGLE and SQLRPGLE programs : in RPG ILE programs the /copy directive is expanded by sql preprocessor, but /include does not get expanded by SQLRPGLE code.

RPGLE use either /INCLUDE or /COPY
SQLRPGLE use /COPY

Why? The /COPY and /INCLUDE directives have the same purpose and the same syntax, but are handled differently by the SQL precompiler. If your program does not have embedded SQL, you can freely choose which directive to use. If your program has embedded SQL then stick with /copy but if it's a native RPGLE program then you can use either.

What is a Copybook?

/copy and /include copybook code is "pulled" into the source by the compiler before executing the compile, this eliminates having to duplicate the code a million times over (but I mainly use for prototypes, data structures...not calculations!).

SQLRPGLE how to update a file with a cursor - WHERE CURRENT OF

$
0
0
so what does WHERE CURRENT OF mean?

So what does WHERE CURRENT OF mean?

Quite simply it means - if we have just read something using an SQL cursor then the operation that we are applying to "where current of" is being applied to the exact data set that just been read.

Let's take an example of direct file access technique for reading a specific record and updating it. How about we read every record in a file with the value of 'X' in s status field and change the value to 'Y'

Read filename;

Dow not %EOF(filename);

If Status = 'X';

   // some program logic stuff could be here

  Status = 'Y';

Endif;

Read filename;

Enddo;

note: this could just as easily be a DELETE operation.

If we wanted to do the same thing in SQL we could do it like this:

Get the IBM i System Name using RPGLE or SQLRPGLE

$
0
0
system names and nicknames are scary in big dark computer rooms

Get Sysname using SQL

The IBM i System Name is not the same thing as the SQL Local DB Server name, although it may be the same by default.  You can retrieve the SQL Local DB Server name using this SQLRPGLE code snippet:

exec sql values current server into :systemName;
If systemName = 'my-ibm-i-system-name';
  do some stuff...
endif;

* updated May 2016 Correction from Steve Croy. Thanks!

 

Get Sysname using CL

The good old tried and tested technique is to write a simple Control Language Program that looks something like this:

dcl &SysName *char
rtvneta sysname( &SysName )

the main drawback with this simple method is that you have another program out there to be called by any/all programs that want to find the system name.

 

Get Sysname using IBM API

Most RPG programmers would probably choose to call the IBM *API to Retrieve Network Attributes (QWCRNETA). This has the same affect as calling a CL to retrieve the network attributes, but of course you could also call the *API from the CLP program if you wanted to be uber-tricky ;)

Modernize RPG code for 'Get the System Name' for IBM i, iSeries and AS400

$
0
0
running free like RPG in big dark computer rooms

For any RPG programmer, writing RPGLE code (sometimes called RPG4) is fun. But, upgrading older RPG progams to the latest free form program layout is even more fun! So lets look at any example:

Adopting Authority - Problems with SQL RPG Programs

$
0
0
SQLRPGLE does not adopt user authority

RPG PROGRAM USER(*OWNER) - Adopts the object owner level of Authority

Most IBM i Programmers are aware of the USER(*OWNER) compile setting -- anyone calling this program will adopt the authority level of the program. So if a program is compiled by user QSECOFR and USER(*OWNER) then anyone calling that program will automatically be elevated to QSECOFR level rights for the duration of that program execution.

This is a simple trick that's sometimes used to create utilities that need specific authority settings.

It's also used to allow users to adopt an authority setting for access to a specific application (without needing to grant authority levels to all the users in question).

Adopting USER(*OWNER) rights lets a user access a database that he/she might usually not be authorised to.... but only when calling that program. You may be able to see that if a user initial program is *OWNER, this can be used to automatically grants specific users rights to an application and its database when they wouldnt normally have that access just from a command line.

Viewing all 13 articles
Browse latest View live


Latest Images