Solved

db2 sql: Is it possible to create table from an existing view, copying data too?

Posted on 2011-03-07
5
794 Views
Last Modified: 2012-05-11
db2 sql: Is it possible to create a table from an existing view, copying both data and structure fields?
0
Comment
Question by:bobdylan75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
Dave Ford earned 400 total points
ID: 35058400

Simple enough ...
create table MyNewTable as (
  select whatever
    from MyView
)
with data

Open in new window

0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 35058402
Hi Bob,

DB2 generally requires that you create the table and populate it in separate steps.  That's a pretty small limitation though.


  CREATE TABLE mytable AS (SELECT * FROM myview) DEFINITION ONLY;

  INSERT INTO mytable SELECT * FROM myview;

You'll have to modify that slightly if the table has an IDENTITY column, and you'll have to reapply any foreign keys, triggers, or constraints that you need.


Good Luck,
Kent
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 35058410
Hi Dave.

'with data'.  When did that come into existence?


(pretty cool)

Kent
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 35058589
Hi Kent,

I’m currently using 6.1 (a.k.a. V6R1) on System i (a.k.a. AS/400). From the SQL Reference manuals, I can see that the "WITH DATA" functionality existed in 5.4 (in 2006) and 5.3 (in 2005), but I don’t see that functionality listed in the 5.1 manual (from 2001).

I no longer have the 5.2 manual, so I can't check that one.

I don't know how that translates to DB2 on other platforms, though.

HTH,
DaveSlash
0
 

Author Closing Comment

by:bobdylan75
ID: 35059128
thanks
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question