Sql server 如何从另一个查询的结果集创建表

Sql server 如何从另一个查询的结果集创建表,sql-server,Sql Server,我正试图从SQL查询的结果集创建一个表,但在select时出现语法错误,并在该点执行了删除操作 这些都是我尝试过的方法 create table schedulewidgetfix as (select * from PRSNLOCPROFMM where PERSONALLOCATNPROFID in (select PERSONALLOCATNPROFID from PERSONALLOCATNPROF where PERSONALLOCATNPROFID not in (sele

我正试图从SQL查询的结果集创建一个表,但在select时出现语法错误,并在该点执行了删除操作

这些都是我尝试过的方法

create table schedulewidgetfix
 as
 (select * from PRSNLOCPROFMM

where PERSONALLOCATNPROFID in

(select PERSONALLOCATNPROFID from PERSONALLOCATNPROF where PERSONALLOCATNPROFID

not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM)))
我尝试的另一种方式是

SELECT *
INTO schedulewidgetfix
FROM

  (select * from PRSNLOCPROFMM

where PERSONALLOCATNPROFID in

(select PERSONALLOCATNPROFID from PERSONALLOCATNPROF where PERSONALLOCATNPROFID

not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM)))
在这里,我得到了结束括号语法错误
那么,创建表的方法是什么呢。您可以像这样向子查询添加别名

SELECT * --you should list the columns here though instead of using *
INTO schedulewidgetfix
FROM

  (select * from PRSNLOCPROFMM

where PERSONALLOCATNPROFID in

(select PERSONALLOCATNPROFID from PERSONALLOCATNPROF where PERSONALLOCATNPROFID

not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM))) x
SELECT * --you should list the columns here though instead of using *
INTO schedulewidgetfix
FROM PRSNLOCPROFMM
where PERSONALLOCATNPROFID in
(
    select PERSONALLOCATNPROFID 
    from PERSONALLOCATNPROF 
    where PERSONALLOCATNPROFID not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM)
)
或者一种更干净的方法是完全像这样消除子查询

SELECT * --you should list the columns here though instead of using *
INTO schedulewidgetfix
FROM

  (select * from PRSNLOCPROFMM

where PERSONALLOCATNPROFID in

(select PERSONALLOCATNPROFID from PERSONALLOCATNPROF where PERSONALLOCATNPROFID

not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM))) x
SELECT * --you should list the columns here though instead of using *
INTO schedulewidgetfix
FROM PRSNLOCPROFMM
where PERSONALLOCATNPROFID in
(
    select PERSONALLOCATNPROFID 
    from PERSONALLOCATNPROF 
    where PERSONALLOCATNPROFID not in (select PERSONALLOCATNPROFID from PRSNLOCPROFORGMM)
)

不要这样做。这是个坏习惯。最佳实践是显式列出所有列。