We help IT Professionals succeed at work.

Problem deleting records returned by a common table expression in db2

DaveChoiceTech
on
1,516 Views
Last Modified: 2014-08-15
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!
Comment
Watch Question

PortletPaulEE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013

Commented:
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

Expert for hire: IBM i, AIX, Linux, Windows, DB2, Performance, Security. EDI
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Gary, thanks a bunch! This kind of feedback is why there is an Expert Exchange.
PortletPaulEE Topic Advisor
CERTIFIED EXPERT
Most Valuable Expert 2014
Awarded 2013

Commented:
That's it then - thanks Gary - good to have it confirmed.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.