×
The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.
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) ;
As an Amazon Associate we earn from qualifying purchases.