使用 WITH 查询执行 INSERT INTO
Perform INSERT INTO with WITH query
我在这个 SO 解决方案 here 中找到了一个查询,并想在我正在处理的一个查询中使用它。解决方案中的示例查询如下:
;with t1 as (
select col1, row_number() over (order by col1) rn
from table1
),
t2 as (
select col2, row_number() over (order by col2) rn
from table2
)
select col1,col2
from t1 full outer join t2 on t1.rn = t2.rn
而不是运行这个查询多次(因为我需要在多个地方使用上述查询的结果)我想做如下的事情:
declare @tempTbl table (col1 int, col2 int)
insert into @tempTbl (col1, col2)
;with t1 as (
select col1, row_number() over (order by col1) rn
from table1
),
t2 as (
select col2, row_number() over (order by col2) rn
from table2
)
select col1,col2
from t1 full outer join t2 on t1.rn = t2.rn
但是那个 ;with
引起了麻烦...求助...
常见的 table 表达式在 之前 插入:
; with t1 as
(
...
)
, t2 as
(
...
)
insert @tempTbl
(col1, col2)
select col1
, col2
from t1
full join
t2
on t1.rn = t2.rn
;
我在这个 SO 解决方案 here 中找到了一个查询,并想在我正在处理的一个查询中使用它。解决方案中的示例查询如下:
;with t1 as (
select col1, row_number() over (order by col1) rn
from table1
),
t2 as (
select col2, row_number() over (order by col2) rn
from table2
)
select col1,col2
from t1 full outer join t2 on t1.rn = t2.rn
而不是运行这个查询多次(因为我需要在多个地方使用上述查询的结果)我想做如下的事情:
declare @tempTbl table (col1 int, col2 int)
insert into @tempTbl (col1, col2)
;with t1 as (
select col1, row_number() over (order by col1) rn
from table1
),
t2 as (
select col2, row_number() over (order by col2) rn
from table2
)
select col1,col2
from t1 full outer join t2 on t1.rn = t2.rn
但是那个 ;with
引起了麻烦...求助...
常见的 table 表达式在 之前 插入:
; with t1 as
(
...
)
, t2 as
(
...
)
insert @tempTbl
(col1, col2)
select col1
, col2
from t1
full join
t2
on t1.rn = t2.rn
;