Jason Baker Jason Baker - 2 months ago 6
SQL Question

Is there any way to create a table with the same layout as a view in SQL Server 2005?

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.

Answer

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