Solved

How can I create a table from a view

Posted on 2014-03-21
7
215 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

825 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