Solved

How can I create a table from a view

Posted on 2014-03-21
7
213 Views
Last Modified: 2014-04-03
I have a view vwData, which has 10 base tables that make up the view.
How can I create a table from the view, keeping the structure of the view

create table newtable
from
(select * from dbo.vwData )
0
Comment
Question by:countrymeister
7 Comments
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 125 total points
ID: 39946675
Something like this:
SELECT *
INTO newtable
FROM  dbo.vwData 

Open in new window


Though why you would want to do that escapes me.
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 125 total points
ID: 39946705
>How can I create a table from the view, keeping the structure of the view
Three thoughts:
1    Anthony said the first one.
2    You do realize that the new table data will be correct only as of when the SELECT .. INTO is executed, so that if any of the 10ish table data changes, the new table is out of synch?

>keeping the structure of the view
3    You may want to explicitly state data types in the SELECT part of the query, to ensure that the data types are as you wish them to be.
0
 
LVL 28

Assisted Solution

by:Ryan McCauley
Ryan McCauley earned 125 total points
ID: 39946862
If you just want a table that has the exact same schema as your view, you'd execute something similar to what Anthony suggested, but with a TOP 0 so you don't insert any actual data:

SELECT TOP 0 *
  INTO YourNewTable
  FROM YourView

That would give you a table with the same layout, but containing no rows.

I'm not sure what problem you're trying to solve, but if it's performance, here's another alternative - if all the tables that make up the view are in the same database as the view, you can actually add indexes on it that are automatically kept current - this would speed up SELECT statements based on the view, and if it's especially complicated, could improve overall performance dramatically (at the cost of slowing down DML for the underlying tables as the view will also have to be updated). To do that, you alter the view to be WITH SCHEMABINDING, then you create a clustered index on the view (this lays the data down on disk), and then you can create any supporting indexes you'd like. Again, not sure what your after, but this is an option if it's performance.

If we're missing your goal, please provide some more details about what you're trying to accomplish and we can give additional guidance.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 1

Author Comment

by:countrymeister
ID: 39947332
Sorry I need to create a real table not a temporary table, the following just creates a temp table

SELECT *
INTO newtable
FROM  dbo.vwData
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39947357
>the following just creates a temp table   ...    SELECT *   INTO newtable
Define 'real'.  This will create a new physical table.  Temp tables are prefixed with pound signs ( # )
0
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 39947874
The T-SQL provided in your comment above will create a persistent table - a "real" table, as you're calling it. As Jim mentions, a temp table is one that is prefaced by # or ##, and is definitely not the case with the code we've both provided.

I get the impression your problem isn't being clearly stated - what are you trying to accomplish? It sounds like the approach might need some adjustment if what we're suggesting isn't meeting your needs.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
ID: 39950923
I think you need an indexed view, as ryan indirectly stated.

There are a lot of restrictions, but if your existing view follows them all, then an indexed view will give you a searchable view that is always up-to-date.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

706 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

19 Experts available now in Live!

Get 1:1 Help Now