473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

question about "select * from new table (update ...)"


I really like the construction:

select * from new table (update ....) X

but I noticed that it cant be used as:

insert into T select * from new table (update ....) X

because of:

SQL20165N An SQL data change statement within a FROM clause is not
allowed
in the context in which it was specified. SQLSTATE=428FL
Anyone (Serge ?) know if there are any plans for loosing up this
restriction in the future, or if it will remain this way?

The other day I had a task updating all rows fullfilling a certain
condition and for each row updated, insert or update (dependent of
existence) another row. A had a depression for 10 minutes when I
discovered I couldnt do it as ;-)

merge into ... using (select ... from new table (update ....) X on
.... when matched ... when not matched ...
/Lennart

Oct 5 '06 #1
5 7587
It's possible today. Try the following:

with tmp1 as (select * from new table(insert into t1 values 1,2,3)
tmp3),
tmp2 as (select * from new table(insert into t2 select * from tmp1)
tmp4)
select count(*) from tmp4

Similar should work for update. Basically, we force the user to specify
the exact order of operations by putting them into Common Table
Expressions, to remove any semantic ambivalence wrt the order of the
execution of the operations.

Hope this helps,
Miro

Oct 5 '06 #2
with tmp1 as (select * from new table(insert into t1 values 1,2,3)
tmp3),
tmp2 as (select * from new table(insert into t2 select * from tmp1)
tmp4)
select count(*) from tmp4
Why is tmp2 necessary?
Is this not enough?
with tmp1 as (select * from new table(insert into t1 values 1,2,3)
tmp3)
select * from new table(insert into t2 select * from tmp1) tmp4;

Oct 6 '06 #3
I'm not 100% certain, but I think DB2 simply disallows a data change
table reference in INSERT, UPDATE and DELETE today. So in principle,
you're right, tmp2 is not necessary, it's there just to get around the
current restriction.

Regards,
Miro

Oct 6 '06 #4

mirof007 wrote:
It's possible today. Try the following:

with tmp1 as (select * from new table(insert into t1 values 1,2,3)
tmp3),
tmp2 as (select * from new table(insert into t2 select * from tmp1)
tmp4)
select count(*) from tmp4
Ah, yes that will do. A little typo though, I think it should be:

with tmp1 as (select * from new table(insert into t1 values 1,2,3)
tmp3),
tmp2 as (select * from new table(insert into t2 select * from tmp1)
tmp4)
select count(*) from tmp2

Also, I think the 2 CTE is unnecessary, because the following works:

# create table
db2 "create table T1 (c1 int not null primary key, c2 int not null
default 0)"
# some init data
db2 "insert into T1 (c1) with T (c1) as (values 0 union all select c1+1
from T where c1 < 5) select c1 from T"
# add some more
db2 "with tmp1 as (select * from new table(update t1 set c2 = c2+1
where mod(c1,2) = 0) tmp3) select * from new table(insert into t1
select c1+10,c2 from tmp1) tmp4"
C1 C2
----------- -----------
10 1
12 1
14 1

[ltjn@lelles ~/install/ddl/bin]$ db2 "select * from T1"

C1 C2
----------- -----------
0 1
1 0
2 1
3 0
4 1
5 0
10 1
12 1
14 1

Next step will be to squeze in the merge, now that will be one hell of
a stmt :-)

Thanx a lot for pointing this out
/Lennart

[...]

Oct 6 '06 #5
Squeezing in MERGE won't work in the same way.
It's the "missing piece".
Much to my dismay SELECT FROM MERGE is not support at present, and WITH
can't precede MERGE.
You can consider using a dynamic compound statement or simply wrapping
the MERGE into an SQL Table function in combination to what Miro proposed.
That should work very well in a non DPF environment.
Make sure to check the db2exfmt.

On the topic of allowing stacked updates those who heard me IBM panel at
IDUG Europe Thursday know that I would like to reduce complexity of SQL
rather than add more to it.
The WITH clause allows for a clean way to semantically serialize these
constructs. Allowing for stacking and joins between updates get very
quickly very unreadable.
We prefer leaving the pipelining to the optimizer.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Oct 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: ian justice | last post by:
Before i post actual code, as i need a speedyish reply. Can i first ask if anyone knows off the top of their head, if there is a likely obvious cause to the following problem. For the moment i've...
16
by: lkrubner | last post by:
Are there any benchmarks on how much an extra, unneeded VARCHAR, CHAR, INT, BIGINT, TEXT or MEDIUMTEXT slows down a database call with MySql? PostGre info would also be useful. I'm trying to...
3
by: Gerlow | last post by:
Hi When I am using a ASP SQL question to my Access 2000 db via IIS 5.0 and have the SELECT TOP 15 syntax in the SQL question I get 15 rows in my answer if a have many rows in the table, but if I...
4
by: Silas | last post by:
Hi, I use view to join difference table together for some function. However, when the "real" table fields changed (e.g. add/delete/change field). The view table still use the "old fields". ...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
7
by: Paul Serby | last post by:
Why does '*select count(id) from "tblContacts"'* do a sequential scan when the field '*id*' is indexed using a btree? MySql simply looks at the index which is keeping a handy record of the...
6
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is...
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
11
by: bcurtu | last post by:
Hi, I have a BIIIIIG problem with the next query: cursor.execute(""" SELECT titem.object_id, titem.tag_id FROM tagging_taggeditem titem WHERE titem.object_id IN (%s) """,( eid_list))
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.