Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 创建cte以标记来自不同加载日期的数据之间的差异_Sql_Common Table Expression_Except - Fatal编程技术网

Sql 创建cte以标记来自不同加载日期的数据之间的差异

Sql 创建cte以标记来自不同加载日期的数据之间的差异,sql,common-table-expression,except,Sql,Common Table Expression,Except,如果发现加载日期1和2之间存在增量,我需要创建一个比较并使用指示器更新标记字段。请指教 示例数据 ID_person, NM_firstname, load_date, marking_field 120, Appleton, 1 120, Apple, 2 120, George, 1 120, George, 2 我目前有一个查询,但这个查询将更新所有行上的标记字段 ;with cte as ( select [ID_person],[NM_firstname] from dbo

如果发现加载日期1和2之间存在增量,我需要创建一个比较并使用指示器更新标记字段。请指教

示例数据

ID_person, NM_firstname, load_date, marking_field
120, Appleton, 1
120, Apple, 2
120, George, 1
120, George, 2
我目前有一个查询,但这个查询将更新所有行上的标记字段

;with cte as (
     select [ID_person],[NM_firstname] from dbo.person where load_date='load1'
     except
     select [ID_person],[NM_firstname] from dbo.person where load_date='load2'
)
update cte

set Marking_Field='10'

谢谢你的建议

实际上,如果人员加载日期为“加载1”,则不能为“加载2”。 因此,您可以使用普通的update语句,而不是将“except”子句与cte一起使用:

update dbo.person
set Marking_Field = '10'
where load_date = 'load1'
但是,如果您不想更新实际的表,而只想在内存中创建一个记录集,则可以使用如下表变量:

declare @a table(id int, firestname varchar(50), marking_field varchar(5));
select [ID_person],[NM_firstname], '10'
into @a
where load_date = 'load1';
select * from @a
或者,如果您想坚持使用cte:

with cte as(
    select [ID_person],[NM_firstname], '10' as marking_field 
    from dbo.person 
    where load_date='load1')
select * from cte;