Solved

How can I create a table from a view

Posted on 2014-03-21
7
219 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
[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
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 66

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 66

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:
Scott Pletcher 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

632 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