0

I am trying to:

  1. Select a row and name it CTE
  2. update some columns for that table CTE

I am implementing this using common table expression, but I cannot get it to work.

Begin
With CTE AS
(select * from MyTable where Column1 is null order by Column2 desc
(Update CTE
Set Column3= 1, Column4 = 1, Column5 = 1))
Commit;

I have tried to follow the DB2 specifications, but still having problems :)

1
  • This is (potentially) dependent on your version (and platform) of DB2, which should be included. Also, please note that unless you have some sort of function that relies on the ordering in the CTE (say, FETCH FIRST 1 ROW ONLY), the ORDER BY will be largely ignored by the rest of the query, and will potentially be removed by the optimizer. Nov 27, 2012 at 17:15

2 Answers 2

1

Normally, the syntax for a CTE looks more like this. Pay attention to the parens.

With CTE AS
(
  select * from MyTable where Column1 is null order by Column2 desc
)
Update CTE
Set Column3= 1, Column4 = 1, Column5 = 1;

But this documentation suggests you can't use an UPDATE statement with a CTE.

You can define a common table expression wherever you can have a fullselect statement. For example, you can include a common table expression in a SELECT, INSERT, SELECT INTO, or CREATE VIEW statement.


Possible workaround

If the CTE isn't updatable, just remove the CTE, and write an UPDATE statement. You don't need ORDER BY at all here. This looks like an equivalent UPDATE statement.

update MyTable
set ...
where Column1 is null
5
  • regarding your answer, was your end conclusion that an UPDATE is not possible for CTE? If so, any ideas on a workaround for this? Nov 27, 2012 at 12:13
  • No, I didn't conclude that UPDATE is impossible, because I think different versions of DB2 have different feature sets. Not just differing version numbers, but I think mainframe versions sometimes differ a little from midrange version and from i386 versions. (I could be wrong about that.) But your posted statement won't work regardless, because of the parens. Test to see whether you can INSERT into the CTE. (INSERT is documented, at least on the page I linked to.) If you can insert, but not update, well, there's your answer. Nov 27, 2012 at 12:32
  • Looks like UPDATE is not possible... damn, there's a lot that can be done with T-SQL but not PL-SQL :) Nov 27, 2012 at 12:55
  • Sorry, I forgot to mention that I am fetching first 1 rows only for the select. Hence, the order by Column2... Nov 28, 2012 at 7:58
  • No, you're fetching all the rows that have NULL in Column1. You're just hoping to ignore a lot of the rows you're fetching. (Your UPDATE statement will honor a WHERE clause, but it won't honor hope.) There are better ways to update a single row. Nov 28, 2012 at 12:33
1

How about this

UPDATE (
  SELECT * FROM MYTABLE WHERE COLUMN1 IS NULL ORDER BY COLUMN2 DESC
  FETCH FIRST 1 ROW ONLY
)
SET COLUMN3= 1, COLUMN4 = 1, COLUMN5 = 1;

or if you want more flexability, maybe my answer on this one How to Update Multiple Queries in optimized way in DB2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.