Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to create a table from querry ( querry table)

Author  Topic 

korssane
Posting Yak Master

104 Posts

Posted - 2009-03-23 : 13:33:00
Hi All,

How can i create a table based on a query ?
i tried with the CREATE Table syntax got several errors?
is there any body that has the correct sysntax or an example?
thanks

heavymind
Posting Yak Master

115 Posts

Posted - 2009-03-23 : 13:37:46
use select into construction instead of create table

Thanks, Vadym
MCITP DBA 2005/2008
Chief DBA at http://www.db-staff.com
Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2009-03-23 : 14:57:17
HI,
Is not "select into " to create a new query only.
i need to create a table from a query..is this the same thing ?

thanks
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-03-23 : 15:24:55
Select Into is to create a table. Try looking in BOL if its not clear.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-23 : 15:32:33
Here is an example:

SELECT *
INTO Table1
FROM Table2
WHERE ...

The above creates Table1 using the schema from Table2 and puts the data returned from the SELECT into this new table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2009-03-23 : 16:35:52
Thanks
this works from table to table. but from view to table ?
should i have to specify the "[view]".
Also is my table gonna be updated automatically as soon as my View changes.?

thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-23 : 17:03:40
You can use a view too:

SELECT *
INTO Table1
FROM View1
WHERE...

I'm not sure what you mean by updated automatically. If you mean will Table1 reflect the View1 if the "schema" of View1 changes, then the answer is no. You'd have to recreate Table1 again.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

korssane
Posting Yak Master

104 Posts

Posted - 2009-03-23 : 20:31:10
hi thansk again for the reply.
i mean is when the view content changes ( i.e record added) the table content will change too.
thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-03-24 : 14:03:18
There is no link between the two objects, so Table1 (from my example) will not get updated unless Table1 is inside the view definition.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -