Sql 将CTE中的数据插入表中
我有以下语句,希望将结果插入到具有相同列的表中:Sql 将CTE中的数据插入表中,sql,sql-server,Sql,Sql Server,我有以下语句,希望将结果插入到具有相同列的表中: with Salary as ( select a.id, a.name, a.Netsalary, a.final_sal from (select * from worker) a left join (select * from Discount) b on a.id = b.id ) select *, Netsalary / fin
with Salary as
(
select
a.id, a.name, a.Netsalary, a.final_sal
from
(select * from worker) a
left join
(select * from Discount) b on a.id = b.id
)
select
*, Netsalary / final_sal SalPer
from
Salary
也许有一个很好的理由使用CTE,也许没有。我宁愿不要。 试试这个
要插入的表的结构是什么?要插入哪些列?只需
insert into table从salary中选择cols
BEGIN TRAN
--You need to put the CTE first and then combine the INSERT INTO
--with your select statement.
--Also, the "AS" keyword following the CTE's name is not optional:
with Salary as
(
select a.id,a.name,a.Netsalary ,a.final_sal
from
(select * from worker) a
left join
(select * from Discount ) b on a.id = b.id
)
INSERT INTO Table_Name (
id,
name,
Netsalary,
final_sal
)
SELECT * FROM Salary
ROLLBACK TRAN
insert into tablename
select
a.id,
a.name,
a.Netsalary,
a.final_sal ,
a.Netsalary / a.final_sal as SalPer
from
worker a
left join Discount b on a.id = b.id
where 1=1
and filterconditions
and filterconditions
and filterconditions