views:

56

answers:

3

I can't really think of the best way to phrase this question, so I'll just give an example. Suppose I have a table that is created like this:

CREATE VIEW People
AS
    SELECT
        id, --int
        name, --varchar(20)
        birthdate --datetime
    FROM SomeTable

If I wanted to change this from a view to a physical table, is there any way to create a table with the same layout?

In other words, I want to take that view and create a table like this:

CREATE TABLE People(
    id int,
    name varchar(20),
    birtdate datetime
)

...but without having to manually write that query out.

This is of course a contrived example. The view has a lot of fields with a lot of different data types, so it would be difficult to do by hand.

+1  A: 

Select * INTO PeopleTable FROM People

KM
Wrong syntax. INTO must be before FROM.
mwigdahl
fixed that already
KM
+2  A: 
SELECT *
INTO People_Table
FROM People_View
mwigdahl
+7  A: 

How about

SELECT * INTO MyNewTable FROM MyView

AND if you don't want the content, just the structure

SELECT * INTO MyNewTable FROM MyView WHERE 1 = 2
Eoin Campbell
Thanks. I figured it would be something simple.
Jason Baker
+1, but just be aware that in some cases, especially when using calculated fields that return null, SQL will not pick the type for the field that you expected. You can use an explicit convert on the field in that case to work around the problem.
Yishai