Try to create a table from Select - SqL Server 2008 throws error

吃可爱长大的小学妹 提交于 2019-11-27 04:48:52

问题


Hi I am trying to create a table using inner select statement...

for example:

CREATE TABLE JmxMonSer AS (SELECT * FROM services WHERE monitoring_enabled = 1);

But keep getting error:

Incorrect Syntax near keyword 'AS', Severity 15

please advice


回答1:


How about:

SELECT * into JmxMonSer FROM services WHERE monitoring_enabled=1

If the table already exists (and the columns types and ordering line up), you can use:

INSERT INTO JmxMonSer SELECT * FROM services WHERE monitoring_enabled=1



回答2:


I'm almost positive that SQL Server doesn't have a CREATE TABLE AS (SELECT... syntax, but you can use SELECT INTO:

SELECT * 
INTO JmxMonSer
FROM services 
WHERE monitoring_enabled=1

Check the MSDN documentation for proper uses of the CREATE TABLE statement.




回答3:


The below syntax is for using sub-query instead of using static table name... because sometime the required result set is coming from different queries..

SELECT * 
INTO JmxMonSer
From (SELECT * FROM services WHERE monitoring_enabled = 1) as X



回答4:


Try using SELECT INTO:

SELECT *
INTO newtable [IN externaldb]
FROM table1;

src: http://www.w3schools.com/sql/sql_select_into.asp



来源:https://stackoverflow.com/questions/11940477/try-to-create-a-table-from-select-sql-server-2008-throws-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!