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 table with a SELECT statement?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-07, 11:42
svrider svrider is offline
Registered User
 
Join Date: Jul 2002
Posts: 39
Create table with a SELECT statement?

Hello everyone,

I have a quick one here. How can I create a table using a SELECT statement? I can't find the right syntax...

In Oracle I'd simply do something like
CREATE TABLE table_name AS
SELECT...

Thanks...
Reply With Quote
  #2 (permalink)  
Old 01-03-07, 13:55
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I am not sure about V9, but in version 8 you can use the create table like syntax, but the new table will have the same columns as the original.

You can insert into with a select from, but that does not create the table.
__________________
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-03-07, 14:52
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
There is no way to create a table based on the columns of a query. The "CREATE TABLE mytable_copy LIKE MyTable" syntax only works for tables, views, or nicknames that are in the catalog.

Are you trying to create a table that has the same columns as the query, or are you trying to create a MQT?

Andy
Reply With Quote
  #4 (permalink)  
Old 01-03-07, 15:27
svrider svrider is offline
Registered User
 
Join Date: Jul 2002
Posts: 39
I'm trying to create a table that has the same columns as the query, basically create a physical view.

It is for testing purposes.

I could always create the table and then do a INSERT INTO / SELECT, but the idea of using the output of a query is faster for temporary tables... too bad...

I'm using v8 by the way.

Thanks fellows!
Reply With Quote
  #5 (permalink)  
Old 01-03-07, 15:31
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You could create a VIEW based on the query, then create the TABLE based on the VIEW:


CREATE VIEW MyView as (SELECT ...)
CREATE TABLE MyTable LIKE MyView

HTH

Andy
Reply With Quote
  #6 (permalink)  
Old 01-03-07, 16:43
dbamota dbamota is offline
Registered User
 
Join Date: Sep 2003
Posts: 237
Try
CREATE TABLE Y AS (SELECT * FROM X ) WITH NO DATA;
INSERT INTO Y SELECT * FROM X;
__________________
mota
Reply With Quote
  #7 (permalink)  
Old 01-03-07, 23:33
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
CREATE TABLE Y AS (SELECT * FROM X ) definition only
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
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