?
Solved

Problem deleting records returned by a common table expression in db2

Posted on 2014-08-14
5
Medium Priority
?
1,017 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!
0
Comment
Question by:DaveChoiceTech
  • 2
4 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40262533
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
 
LVL 36

Accepted Solution

by:
Gary Patterson earned 2000 total points
ID: 40263006
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
 

Author Closing Comment

by:DaveChoiceTech
ID: 40263037
Gary, thanks a bunch! This kind of feedback is why there is an Expert Exchange.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40263038
That's it then - thanks Gary - good to have it confirmed.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question