Solved

How can I create a table from a view

Posted on 2014-03-21
7
218 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

734 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