|
On 19 Dec 2012 14:29, Jeff Young wrote:
I want to use SQL to determine if a record exists in a file for a
given value.
There will be either only 1 record in the file for the value, or it
will not exist.
Given a variable [named CmpVal] that is compatible with the column
[named TheCol] of the TABLE [named TheFile], then the following SQL
embedded statement can be used:
D ind S 1A inz('?')
D* "ind" if Row Exists: '1'=True, '0'=False, '?'=InquiryFailed
exec sql
set ind=coalesce( (select '1'
from TheFile
where TheCol=:CmpVal)
, '0' ) ;
Of course like any SQL, the return code is best still checked,
although in the above given setup, any failure of the statement yields
'?' irrespective of the type of error. For example sqlcode=-811 if the
uniqueness were not a valid assumption ¿and use of the FETCH FIRST ROW
ONLY may not be not allowed for the scalar fullselect? Other errors
could be file not found, column not found, selection\conversion data
errors, etc. Using an indicator is an alternative, although then the
test of the sqlcode or sqlstate is even more relevant, to know that a
false result is due to an error versus due to no row being selected.
Or in an SQL procedure, perhaps:
/* ind inout char, cmpval in ... */
/* "ind" if Row Exists: '1'=True, Null=False, '?'=InquiryFailed
declare CONTINUE HANDLER for SQLEXCEPTION set ind = '?';
set ind=(select '1' from TheFile where TheCol=CmpVal) ;
--
Regards, Chuck
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.