求一条数据替换的查询语

lancelang 2006-06-09 01:43:13
有表A和表B,两个表的字段一样,如果更改了表B中的数据,如何对比两个表查找出那些更改过的记录。
如果有游标的话速度太慢,可不可以不用游标呢?

...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lancelang 2006-06-09
  • 打赏
  • 举报
回复
谢谢 LouisXIV(夜游神)
我自己已经搞定了,和你的方法差不多,:)
LouisXIV 2006-06-09
  • 打赏
  • 举报
回复
两种方案,自己选
------------------------------------------------------
declare @a table
(a int,
b int)

declare @b table
(a int,
b int)

insert into @a
select 1,1
union all
select 2,2
union all
select 3,3
union all
select 4,4

insert into @b
select 1,1
union all
select 2,2
union all
select 3,3
union all
select 4,4

select * from @b
select * from @b where checksum(*) not in (select checksum(*) from @a)

update @b
set b=5
where a=4
select * from @b
select * from @b where checksum(*) not in (select checksum(*) from @a)

---------------------------------------------------------------------

declare @a table
(a int,
b int)

declare @b table
(a int,
b int)

insert into @a
select 1,1
union all
select 2,2
union all
select 3,3
union all
select 4,4

insert into @b
select 1,1
union all
select 2,2
union all
select 3,3
union all
select 4,4

select * from @b
select * from @b where checksum(*) not in (select checksum(*) from @a)
select * from @b b where not exists (select a,b from @a where a=b.a and b=b.b)
update @b
set b=5
where a=4
select * from @b
select * from @b where checksum(*) not in (select checksum(*) from @a)
select * from @b b where not exists (select a,b from @a where a=b.a and b=b.b)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧