Solved

How can I create a table from a view

Posted on 2014-03-21
7
214 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 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

17 Experts available now in Live!

Get 1:1 Help Now