Make &able with" with"

时间:2015-10-21 08:03:47

标签: sql sql-server common-table-expression

我尝试结合"选择*进入"用"用"

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
)
  

错误:关键字'附近的语法错误,用'。

我如何解决此错误?

2 个答案:

答案 0 :(得分:2)

您需要将公用表表达式放在实际的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;

答案 1 :(得分:1)

可以找到公共表表达式的正确语法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;