Unix Technical Forum

move rows from one table to another

This is a discussion on move rows from one table to another within the DB2 forums, part of the Database Server Software category; --> Maybe a simple question, but what is the best way to move (not copy!) data from one table to ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-24-2008, 07:09 AM
Frank Swarbrick
 
Posts: n/a
Default move rows from one table to another

Maybe a simple question, but what is the best way to move (not copy!) data
from one table to another?
Meaning, I want to insert a set of rows from one table to another *and*
delete all of the same rows from the source table.

I tried the following, but DB2 does not allow it:

INSERT INTO FILM_TRANSACTIONS2
SELECT *
FROM OLD TABLE (
DELETE FROM FILM.FILM_TRANSACTIONS
WHERE POST_DATE = CURRENT_DATE
);

DB2 says:
SQL20165N An SQL data change statement within a FROM clause is not allowed
in the context in which it was specified.

Obviously I can do this:
INSERT INTO FILM_TRANSACTIONS2
SELECT *
FROM FILM.FILM_TRANSACTIONS
WHERE POST_DATE = CURRENT_DATE;

DELETE FROM FILM.FILM_TRANSACTIONS
WHERE POST_DATE = CURRENT_DATE;

But I thought it might be better to do both in a single statement.
(Maybe not!)

Thanks,
Frank

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-24-2008, 07:09 AM
jefftyzzer
 
Posts: n/a
Default Re: move rows from one table to another

On May 23, 10:34 am, "Frank Swarbrick"
<Frank.Swarbr...@efirstbank.com> wrote:
> Maybe a simple question, but what is the best way to move (not copy!) data
> from one table to another?
> Meaning, I want to insert a set of rows from one table to another *and*
> delete all of the same rows from the source table.
>
> I tried the following, but DB2 does not allow it:
>
> INSERT INTO FILM_TRANSACTIONS2
> SELECT *
> FROM OLD TABLE (
> DELETE FROM FILM.FILM_TRANSACTIONS
> WHERE POST_DATE = CURRENT_DATE
> );
>
> DB2 says:
> SQL20165N An SQL data change statement within a FROM clause is not allowed
> in the context in which it was specified.
>
> Obviously I can do this:
> INSERT INTO FILM_TRANSACTIONS2
> SELECT *
> FROM FILM.FILM_TRANSACTIONS
> WHERE POST_DATE = CURRENT_DATE;
>
> DELETE FROM FILM.FILM_TRANSACTIONS
> WHERE POST_DATE = CURRENT_DATE;
>
> But I thought it might be better to do both in a single statement.
> (Maybe not!)
>
> Thanks,
> Frank


Frank:

Have a look at slide 16 of part two of Serge's "SQL on Fire"
presentation. You can do the insert from a delete, but you have to do
it via a CTE.

--Jeff
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-24-2008, 07:09 AM
Frank Swarbrick
 
Posts: n/a
Default Re: move rows from one table to another

>>> On 5/23/2008 at 1:28 PM, in message
<59c28351-0535-4835-bc84-6e1163c6ac5b@p25g2000pri.googlegroups.com>,
jefftyzzer<jefftyzzer@sbcglobal.net> wrote:
> On May 23, 10:34 am, "Frank Swarbrick"
> <Frank.Swarbr...@efirstbank.com> wrote:
>> Maybe a simple question, but what is the best way to move (not copy!)

> data
>> from one table to another?
>> Meaning, I want to insert a set of rows from one table to another *and*
>> delete all of the same rows from the source table.
>>
>> I tried the following, but DB2 does not allow it:
>>
>> INSERT INTO FILM_TRANSACTIONS2
>> SELECT *
>> FROM OLD TABLE (
>> DELETE FROM FILM.FILM_TRANSACTIONS
>> WHERE POST_DATE = CURRENT_DATE
>> );
>>
>> DB2 says:
>> SQL20165N An SQL data change statement within a FROM clause is not

> allowed
>> in the context in which it was specified.
>>
>> Obviously I can do this:
>> INSERT INTO FILM_TRANSACTIONS2
>> SELECT *
>> FROM FILM.FILM_TRANSACTIONS
>> WHERE POST_DATE = CURRENT_DATE;
>>
>> DELETE FROM FILM.FILM_TRANSACTIONS
>> WHERE POST_DATE = CURRENT_DATE;
>>
>> But I thought it might be better to do both in a single statement.
>> (Maybe not!)
>>
>> Thanks,
>> Frank

>
> Frank:
>
> Have a look at slide 16 of part two of Serge's "SQL on Fire"
> presentation. You can do the insert from a delete, but you have to do
> it via a CTE.


Great! I don't know that I ever would have come up with it on my own, but
here's what I have now.

WITH
DEL(brch_nbr, acct_nbr, last_stmt_date, post_date, amount, serial_nbr
, sequence_nbr, post_flag, null_test, update_date) AS (
SELECT *
FROM OLD TABLE (
DELETE FROM FILM.FILM_TRANSACTIONS
WHERE POST_DATE = '09/14/2006'
)
)
, INS(x) AS (
SELECT 1
FROM NEW TABLE (
INSERT INTO FILM.FILM_TRANSACTIONS2
SELECT *
FROM DEL
)
)
SELECT COUNT(1)
FROM INS;

Works like a charm!

Thanks,
Frank

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:43 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com