If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Create a table from another table with data

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-04-11, 18:04
mrcool4 mrcool4 is offline
Registered User
 
Join Date: Jan 2011
Posts: 3
Create a table from another table with data

Hi All,

I am new t0 Db2.
I am creating a table from another table as below:

Create table New Like Old

Insert into New select * from old

Is there any better way than doing above.

I am using below query which is not working in DB2

Create table new as select * from old;

Please let me know if there are any better options.

Thanks,
Mrcool
Reply With Quote
  #2 (permalink)  
Old 01-04-11, 23:25
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I realize that having to submit two SQL statements (one to clone the table, and another insert the data from the other table) means your life is most likely ruined, but I don't think you can do this with one SQL statement in DB2 (unless there is a new feature I don't know about).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 01-04-11, 23:29
mrcool4 mrcool4 is offline
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks Marcus. But is there any other approach than what I am doing.

Thanks,
Mrcool
Reply With Quote
  #4 (permalink)  
Old 01-04-11, 23:43
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
The only other way I know of is to EXPORT the data from first table using IXF format, and then IMPORT into a new table using the REPLACE_CREATE option. This will create the table using the table definition contained in the export file (IXF format only) if the table does not already exist. If you have more than 50,000 rows, I would use the COMMITCOUNT option on the IMPORT.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 01-04-11, 23:53
mrcool4 mrcool4 is offline
Registered User
 
Join Date: Jan 2011
Posts: 3
Once Again Thanks for your inputs Marcus. It is helpfull.

Cheers
Mrcool
Reply With Quote
  #6 (permalink)  
Old 01-05-11, 04:51
nvk@vhv nvk@vhv is offline
Registered User
 
Join Date: Jan 2010
Posts: 294
With load you could bypass the transaction log.
declare load_cur cursor for select * from old;
load from load_cur of cursor insert into new nonrecoverable;

But that would be 3 Statements. On the other hand it's a lot faster than Import or Insert for large amonut of data.

Caution! Nonrecoverable means that the data is not recoverable in case of restore.
The "create table ... like ..." does not copy any Indexes, Constraints etc.

Cheers
nvk
Reply With Quote
  #7 (permalink)  
Old 10-04-11, 04:01
mig221 mig221 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
Simply use the "with data" option"
For example
create table test as (select * from xxxusersxxx) with data
Reply With Quote
  #8 (permalink)  
Old 10-04-11, 04:55
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
create table test as (select * from xxxusersxxx) with data

was this tested ?? there is a mandatory option : with no data according the syntax diagram - not seen this option before
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #9 (permalink)  
Old 10-06-11, 15:40
mig221 mig221 is offline
Registered User
 
Join Date: Oct 2011
Posts: 2
I was actually using it when I typed the message. So I can say that I've tested it.
Reply With Quote
  #10 (permalink)  
Old 10-06-11, 16:46
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Looking into manuals,
DB2 for iSeries(at least V5R4) supports WITH DATA in as-subquery-clause, like...
AS (select-statement) WITH DATA

DB2 10 for z/OS and DB2 9.7 for LUW don't support WITH DATA.
Reply With Quote
  #11 (permalink)  
Old 10-06-11, 16:47
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
That gives me an error: (DB2 LUW 9.5)

Code:
DB2INST1:LCAD-->create table andy.call_Center as (select * from production.call_center) with data
DB21034E  The command was processed as an SQL statement because it was not a
valid Command Line Processor command.  During SQL processing it returned:
SQL0104N  An unexpected token "data" was found following "on.call_center)
with".  Expected tokens may include:  "NO".  SQLSTATE=42601
I have checked the syntax diagrams for LUW 9.5 and 9.7 and neither have this option. They only have "WITH NO DATA". I even check DB2 for Z/OS and the syntax does not show it.

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On