Problem deleting records returned by a common table expression in db2

I apologize in advance for the involved query below but I couldn't find a way to make it clearer.

When I run this query it selects the MAP_TRANCODE_ID's to be removed

----------------
with CTE1 as
(
select MAP_TRANCODE_ID, MAP_ID, TRANCODE, MAP_TYPE, DELETE_FLAG, FILE_GROUP_CODE, CATEGORY, FILE_FORMAT_ID, DATE_FORMAT_OPTION, MATCHING_KEY_ID, MAKE_ACCOUNT_OPTION, row_number()
over (PARTITION BY MAP_ID, TRANCODE, MAP_TYPE, DELETE_FLAG, FILE_GROUP_CODE, CATEGORY, FILE_FORMAT_ID, DATE_FORMAT_OPTION, MATCHING_KEY_ID, MAKE_ACCOUNT_OPTION order by MAP_TRANCODE_ID) cte1_RN
from TLF_MAP_TRANCODE
where DELETE_FLAG = 'N'
)
,
CTE2 as
(
select cte1_2.MAP_TRANCODE_ID, cte1_2.MAP_ID, row_number() 
over (PARTITION BY cte1_2.MAP_ID) cte2_RN
FROM CTE1 cte1_1
Join CTE1 cte1_2
on cte1_2.MAP_ID = cte1_1.MAP_ID
where cte1_1.cte1_RN > 1
and cte1_2.MAP_TRANCODE_ID in
(
select MT.MAP_TRANCODE_ID AS "MT.MAP_TRANCODE_ID"
from FTC.TLF_MAP_TRANCODE MT LEFT JOIN FTC.TLF_FILE_ATTRIBUTE FA
on MT.MAP_TRANCODE_ID = FA.MAP_TRANCODE_ID
where FA.MAP_TRANCODE_ID is null
)
)

select MAP_TRANCODE_ID from TLF_MAP_TRANCODE
where MAP_TRANCODE_ID in 
(
select MAP_TRANCODE_ID FROM CTE2
where CTE2_RN = 1
)

Open in new window

----------------

When I replace the last part
select MAP_TRANCODE_ID from TLF_MAP_TRANCODE
where MAP_TRANCODE_ID in
(
select MAP_TRANCODE_ID FROM CTE2
where CTE2_RN = 1
)
with this:
 
delete from TLF_MAP_TRANCODE
where MAP_TRANCODE_ID in
(
select MAP_TRANCODE_ID FROM CTE2
where CTE2_RN = 1
)
I get the error
"An unexpected token "delete" was found following "ODE_ID is null  )  )".  Expected tokens may include:  "<from>".  SQLSTATE=42601"

I don't get it!
DaveChoiceTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
admission: I'm not a DB2 user, but

Using CTEs with DELETE might be DB2 version dependent, or not supported

Judging by this old v8.2.0 reference WITH  is used for isolation level, and hence there may be problems attempting to use a CTE as well.  The v10.5.0 reference is much the same.

You may need to revert to derived tables (subqueries) instead (yes I can see you re-use a CTE) & I do repeat I'm not a DB2 expert.

an indented version:
WITH
      CTE1
      AS (
                  SELECT
                        MAP_TRANCODE_ID
                      , MAP_ID
                      , TRANCODE
                      , MAP_TYPE
                      , DELETE_FLAG
                      , FILE_GROUP_CODE
                      , CATEGORY
                      , FILE_FORMAT_ID
                      , DATE_FORMAT_OPTION
                      , MATCHING_KEY_ID
                      , MAKE_ACCOUNT_OPTION
                      , ROW_NUMBER()
                        OVER (PARTITION BY MAP_ID, TRANCODE, MAP_TYPE, DELETE_FLAG, FILE_GROUP_CODE, CATEGORY, FILE_FORMAT_ID, DATE_FORMAT_OPTION, MATCHING_KEY_ID, MAKE_ACCOUNT_OPTION ORDER BY MAP_TRANCODE_ID) cte1_RN
                  FROM TLF_MAP_TRANCODE
                  WHERE DELETE_FLAG = 'N'
            ),
      CTE2
      AS (
                  SELECT
                        cte1_2.MAP_TRANCODE_ID
                      , cte1_2.MAP_ID
                      , ROW_NUMBER()
                        OVER (PARTITION BY cte1_2.MAP_ID) cte2_RN --<<  ORDER BY REQUIRED?
                  FROM CTE1 cte1_1
                        JOIN CTE1 cte1_2
                                    ON cte1_2.MAP_ID = cte1_1.MAP_ID
                  WHERE cte1_1.cte1_RN > 1
                        AND cte1_2.MAP_TRANCODE_ID IN (
                              SELECT
                                    MT.MAP_TRANCODE_ID AS "MT.MAP_TRANCODE_ID"
                              FROM FTC.TLF_MAP_TRANCODE MT
                                    LEFT JOIN FTC.TLF_FILE_ATTRIBUTE FA
                                                ON MT.MAP_TRANCODE_ID = FA.MAP_TRANCODE_ID
                              WHERE FA.MAP_TRANCODE_ID IS NULL
                        )
            )

SELECT
      MAP_TRANCODE_ID
FROM TLF_MAP_TRANCODE
WHERE MAP_TRANCODE_ID IN (
            SELECT
                  MAP_TRANCODE_ID
            FROM CTE2
            WHERE CTE2_RN = 1
      )

Open in new window

0
Gary PattersonVP Technology / Senior Consultant Commented:
You can't reference a CTE in a DELETE statement in any version of DB2 that I'm familiar with.  You can use CTEs before a fullselect, an INSERT, or CREATE VIEW.  UPDATE, and DELETE, in particular, are not permitted.

http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0059217.html?cp=SSEPGG_10.5.0%2F2-9-6-3-0&lang=en

You'll have to either convert the SQL statement to eliminate the CTE expressions and get it inline in the WHERE clause, or output to an intermediate table and then use that table to do the delete.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveChoiceTechAuthor Commented:
Gary, thanks a bunch! This kind of feedback is why there is an Expert Exchange.
0
PortletPaulfreelancerCommented:
That's it then - thanks Gary - good to have it confirmed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DB2

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.