Solved

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

Posted on 2011-03-07
5
772 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
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
daveslash 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:Kdo
Kdo 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:Kdo
ID: 35058410
Hi Dave.

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


(pretty cool)

Kent
0
 
LVL 18

Expert Comment

by:daveslash
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now