使用 "with" 创建表格

MakeTable with "with"

我尝试将 "select * into" 与 "with"

结合起来
drop table #a
create table #a(a1 int);

insert into #a (a1) values (1),(2);


select * into #b(--I try this syntax and have error mentioned below
    with a as
    (
        select * from #a
    )
    select * from a
)

Error: Incorrect syntax near the keyword 'with'.

如何解决这个错误?

可以找到常见 table 表达式的正确语法 here:

drop table #a 
create table #a(a1 int)

insert into #a (a1) values (1),(2)


;with a as (
    select * from #a
) 
select * into #b from a;

您需要将通用的 table 表达式放在实际的 insert:

之前
drop table #a;
create table #a(a1 int);

insert into #a (a1) values (1),(2);

with a as (
  select * 
  from #a
)
select * 
  into #b
from a;