On 5/18/2012 11:25 AM, ga wrote:
> Jonathan,
>
> Well, the database might be ugly but then again, maybe not as bad as
> you think... :-) :-) I have lots of files that reference my
> ITEMMAST and the key is MFR/ITEM. I'm not sure how I could design the
> files but anyway, that's a different can of worms.
Are these 90 files all different logical files that reference the same
underlying physical file?
>
> Here is my EVAL clause I am working with now:
>
> C**********************************************************
> C EVAL statement = 'Select * '+
> C 'From ' + %trim(FILE) +
> C ' where ' +
> C %trim(MIFLD) + ' = ''' + %trim(MIOLD) +
> C ''' AND ' + %trim(PNFLD) + ' = ''' +
> C %trim(PNOLD) + ''''
> C**********************************************************
>
> I don't need to do a super long thing like you suggested because I at
> least put the FILE, MIFLD and PNFLD into arrays so I just simply loop
> through them and sub in the array element data. All I want to know is
> if any record has a match with the MIOLD and PNOLD fields I am
> checking.
>
> Your example is helpful except you lost me at sysdummy1 - I'd like you
> to know, yes, I do "resemble" that remark...by the way.
Heh heh heh...a misunderstanding. SYSDUMMY1 is a dummy table, like DUAL
in Oracle, that you can use when you need a dummy table for a FROM
clause. In some implementations of SQL, e.g. T-SQL, you don't need a
FROM clause; you can code something like
select '1' into :host_variable
where [some selection criteria]
You can't do that in DB2. DB2 supports a VALUES or a SET statement, but
you can't specify selection criteria. To do that, you need to code a
SELECT, and to code a SELECT you need a FROM. SYSDUMMY1 is an IBM
supplied table in SYSIBM that contains a single row with a single column
(IBMREQD; has a value of 'Y'). You can select a literal you want, which
is a good way to set an indicator or some other variable based on some
condition:
select '1' into :my_indicator
where [some selection criteria]
>
> In reading your code, it appears I need to modify my EVAL something
> like:
>
> SELECT '1' (is this forcing an indicator to on if it gets a match?)
> from SYSIBM/SYSDUMMY1 (what is this?????)
> into:*IND11
> From FILE
> WHERE exists (what is this doing?)
> select 'Y' (what is this doing)
> where
> mifld = miold
> pnfld = pnold
Not quite. If you're going to loop through an array containing 90 files
and variable names (and values), then you should use a cursor, which
will allow you to simplify your SELECT statement, specifically by
getting rid of the subquery in the EXISTS clause. Your statement
variable will look (after you do your EVAL) something like this:
select '1'
from FILE
where mifld = miold
and pnfld = pnold
except that 'miold' and 'pnold' would be the values of the variables
instead.
You'd also have these:
Exec SQL
Declare Check_Mfr_Item cursor for Prepared_Cursor_Statement;
Exec SQL
Prepare Prepared_Cursor_Statement from :Statement;
Exec SQL
Open Check_Mfr_Item;
Mfr_Item_Exists = *off;
Exec SQL
Fetch Check_Mfr_Item into :Mfr_Item_Exists;
Exec SQL
Close Check_Mfr_Item;
Of course, you'd probably want to include some checks of SQLSTT or
SQLCOD for your open and fetch. Note that you only have to attempt to
fetch one row from the cursor each time. At the conclusion of an
iteration, if Mfr_Item_Exists is on, then the value exists in the file;
if it's off, then the value doesn't exist. From what you said below, if
it comes on at any point, you're done and you can end your loop.
You could also define the cursor using parameter markers, which might
simplify your EVAL statement somewhat. That would be most suitable if
the column names (mifld, pnfld) are the same in every file. Then you
could have your statement variable hardcoded something like
select '1'
from XXXXXXXXXX
where mifld = ?
and pnfld = ?
Assuming the file name placeholder XXXXXXXXXX is at position 17, you'd
then code
%subst(statement:17:10) = file_arr_elem;
Then your cursor open statement would be
Exec SQL
Open Check_Mfr_Item using :miold, :pnold;
You could do this even if the column names changed from one file to the
next, but if you're having to string the column names into the statement
variable, you might as well string the values for them in as well.