0

I'm attempting to use a MERGE statement to update records in a physical file on an iSeries or AS400 running V7R1.

The issue that I am having is when attempting to use the merge on the test table, I get a Row not found for MERGE. SQLSTATE=02000 warning very similar to this question on SO.

The main difference in my issue is that I started by creating a copy of my test table to preserve the data, and then using the merge to update. The merge worked against the copy created with CPYF. When I was satisfied the merge was working as expected, I pointed it to the proper test file and upon execution, received the warning below.

SQL State: 02000
Vendor Code: 100 Message: [SQL0100] Row not found for MERGE.

The target table is a membered physical file with an alias over it. For example, DOUG/MYDATA(F901), to create the alias I ran the following:

CREATE ALIAS DOUG.MYDATA901 FOR DOUG.MYDATA(F901);

Query that is not updating is below:

MERGE INTO DOUG.MYDATA901 TGT USING DOUG.TEST01 SRC
    ON TGT.PREC = SRC.PREC
WHEN MATCHED THEN
    UPDATE SET TGT.PQDS = TGT.PQDS - SRC.TTL_DIST;

However, when I make a copy of the file I'd like to update, the merge works...

CPYF FROMFILE(DOUG/MYDATA) TOFILE(DOUG/BUMYDATA) FROMMBR(F901) MBROPT(*REPLACE) CRTFILE(*YES) OUTFMT(*CHAR)

Merge code yields modified records:

MERGE INTO DOUG.BUMYDATA TGT USING DOUG.TEST01 SRC
    ON TGT.PREC = SRC.PREC
WHEN MATCHED THEN
    UPDATE SET TGT.PQDS = TGT.PQDS - SRC.TTL_DIST;

I validated that the source and copy, when used in a select with an inner join, does in fact yield results:

SELECT *
FROM DOUG.MYDATA901 TGT
JOIN DOUG.BUMYDATA SRC ON TGT.PREC = SRC.PREC;

Why would the MERGE work for the copied data but not the source table I created the copy from?

Manikandan
  • 1,195
  • 8
  • 26
Douglas Korinke
  • 389
  • 7
  • 20

1 Answers1

1

When you're making your copy, you're only copying a single member of the file...FROMMBR(F901)

To copy all members, you'd need to use FROMMBR(*ALL) TOMBR(*FROMMBR)

I'm not aware, nor could I find any documentation, of any limitation of MERGE for multi-member files. I did find the following in the v7.1 SQL Reference for ALIAS

An alias that refers to an individual partition of a table or member of a database file can only be used in a select statement, CREATE INDEX, DELETE, INSERT, MERGE, SELECT INTO, SET variable, UPDATE, or VALUES INTO statement.

So it seems the ALIAS should have worked, however, there are change markers in the docs around that block. MERGE was added at 7.1, so the change markers might be from the initial enhancement, or perhaps ALIAS support was added a bit later.

I'd recommend asking IBM, but 7.1 is out of support.

You could create a copy of your aliased file in QTEMP with data and merge your updates into that file. Then use QSYS2.QCMDEXC to run a CPYF command and replace that data in your physical file member.

Douglas Korinke
  • 389
  • 7
  • 20
Charles
  • 21,637
  • 1
  • 20
  • 44