使用带有 CTE 和排名功能的插入时出错

Error when using insert with CTE and ranking function

我有一个 table temp.Results,其中包含员工信息。 table 包含 HR 使用的信息。对员工记录的所有更改都在 table.i.e.

select * from temp.Results where ID=1
1,'2 main st','salem','2009-01-01','2000-01-01'
1,'34 elm st','acton','2013-03-09','2000-01-01'

Datevalidated 是我们输入最新的时间info.DateProcessed是我们第一次输入员工信息。

 WITH ordered as( 
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
       order by DateValidated desc) as rn from 
  aa.temp.Results (nolock) where id=31  
  ) 
  insert into tempResults2(ID, name, address,city, DateValidated, DateProcessed)
select ID, name, address,city, DateValidated, DateProcessed from ordered where rn = 1 ;

我尝试获取上述查询以将每个员工的最新信息放入一个 teable 中,但出现此错误。

Invalid object name 'tempResults2'

如何解决?

谢谢 先生

您的查询有误。什么时候可以像这样使用nolock

WITH ordered as( 
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
       order by DateValidated desc) as rn from 
  aa.temp.Results with(nolock) where id=31  
  ) 

您可以将 where 子句与 select * into 一起使用。

WITH ordered as
( 
    select 
        ID, name, address,city, DateValidated, DateProcessed,
        ROW_NUMBER() over (partition by DateValidated order by DateValidated desc) as rn 
    from aa.temp.Results  where id=31  
  ) 

select * into tempResults2 from ordered where rn = 1 ;
WITH ordered as( 
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
       order by DateValidated desc) as rn from 
  aa.temp.Results (nolock) where id=31  
  ) 
  select ID, name, address,city, DateValidated, DateProcessed 
  into tempResults2 
  from ordered 
  where rn = 1 ;