473,385 Members | 1,356 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,385 software developers and data experts.

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

Jun 27 '08 #1
2 9861
On May 23, 10:34 am, "Frank Swarbrick"
<Frank.Swarbr...@efirstbank.comwrote:
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
Jun 27 '08 #2
>>On 5/23/2008 at 1:28 PM, in message
<59**********************************@p25g2000pri. googlegroups.com>,
jefftyzzer<je********@sbcglobal.netwrote:
On May 23, 10:34 am, "Frank Swarbrick"
<Frank.Swarbr...@efirstbank.comwrote:
>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

Jun 27 '08 #3

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

Similar topics

1
by: anonieko | last post by:
This example applies to javascript, table, cells, rows > > How do you access rows and columns of a HTML table? > > > <script language="javascript"> alert('start');
7
by: tshad | last post by:
Is there a way to move a row in a Datalist up or down without having to re-read the data? I have a datalist which has embedded Datagrids in it. I want to allow the user to move a row up or down...
1
by: Phil Endecott | last post by:
Dear Postgresql experts, According to the documentation for MOVE, it returns the number of rows that it has moved over. It seems to me that this is true for MOVE FORWARD n, but not for MOVE...
14
by: Peter | last post by:
Is there a fast way to move data from DataTable into double array, or do I have to spin through every record and column? I have five tables and I need to merge these tables column wise, each table...
3
by: Eric | last post by:
When i run my query it transfer last 4 digits of account number from one table to another and its wrong. There are two tables one i use for parsing. Second thru query i use to move data from temp...
6
by: NarutoFanatic | last post by:
Hi! I need help in MS ACCESS, I need to create a query that could move different rows into another column. More like concatenation but in a different column rather than in one column. Example: ...
8
by: Lee | last post by:
guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will...
3
by: dianaj86 | last post by:
I have multiple dropdownlists each one filled with values from a specific column in the table. Also I have multiple textboxes corresponding to dropdownlists. For example, when I select an item from...
11
by: sanju | last post by:
Dear All, I have html table and this table contains 10 Rows and 2 column, I want every time this HTML page is called by the user to view the rows Randomly. How can I do this from JavaScript? ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.