[Except]How to compare the rows of two tables and fetch the differential data.
思韵闪耀
2013-11-23
0
In this blog I would like to demonstrate a scenario where users want to move the changes between the tables in two different databases.
Let’s say we would like to compare and move the changes between the databases for some tables using T-SQL
The below example talks about moving the changes between the two databases in the same instance However the same can be extended across instances if you use linked server or SSIS packages.
Also we can write queries to move the DML changes from source to destination and vice versa. Let’s look at the below example

--creating a source database
create database source

--create source  table
use source

create table Product(

Pid int  primary key ,
Pname varchar (10),
Pcost float,
source int ,
location varchar(10))

--create destination database

create database Destination

--create destination table

use Destination

create table Product(

Pid int  primary key ,
Pname varchar (10),
Pcost float,
source int,
location varchar(10) )

--Insert data into source table
use source

insert into product values  ( 1,'rdbms',100,200,'ind')
insert into product values  ( 2,'dbm',20,100,'US')
insert into product values  ( 3,'arp',30,250,'UK')
insert into product values  ( 4,'mqr',40,100,'ind')
insert into product values  ( 5,'ttp',50,200,'us')

-- EXCEPT returns any distinct values from the left query that are not also  found on the right query.
--The below query gives us difference between sourec and destination
-- we can use except ket word to look at selected columns or entire table

select * from source.dbo.product

except

select * from [Destination].dbo.product

--updating destination table with the changes from source

insert into [Destination].dbo.product
select * from source.dbo.product
except
select * from [Destination].dbo.product

-- We see that the destination is populated with all the rows from source

select * from [Destination].dbo.product

--Now lets update the row in the source and see how it works

update source.dbo.product
set pname='sql'
where pid =1
--run the below query
select * from source.dbo.product

except

select * from [Destination].dbo.product

-- the result gives us the only row which was changed in source

-- loading the deiffrences to a temp table
select * into #temp from source.dbo.product

except

select * from [Destination].dbo.product

--updating the destination with changes

update [Destination].dbo.product
set [Destination].dbo.product.pname= #temp.pname
from #temp where #temp.pid= [Destination].dbo.product.pid

--lets run the statement to see the difference between these tables

select * from source.dbo.product

except

select * from [Destination].dbo.product

--lets see how the delete works

delete from source.dbo.product where pid= 2

-- to see the rows which were deleted at source or inserted at destination only
select * from [Destination].dbo.product
except
select * from source.dbo.product
--based on the application logic either we will insert it back in the source or delete from dest

--lets say we want to delete from dest as well ,

select * into  #temp from [Destination].dbo.product
except
select * from source.dbo.product

delete from [Destination].dbo.product where pid in ( select pid from #temp)

-- Now lets see that difference between the tables
select * from [Destination].dbo.product
except
select * from source.dbo.product

来自:How to compare the rows of two tables and fetch the differential data.


【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

相关内容

docker-compos...
1.Compose介绍 DockerCompose是一个用来定义和...
2024-04-26
firewall-cmd ...
firewalld的简要说明:firewalld 、firewal...
2024-03-15
centos7 firew...
名词解释 在具体介绍zone之前学生先给大家介绍几个相关的名词,因...
2023-12-22
数字证书和加密Exchan...
加密和数字证书是所有组织的重要考虑因素。 默认情况下,Exchan...
2022-09-16
续订Exchange Se...
每个证书都有内置的到期日期。 在Exchange Server中,...
2022-09-16
如何续订Exchange中...
安装 Exchange 服务器时会默认创建三张自签名证书: Mic...
2022-09-16

热门资讯

sql中int型与varcha... sql中int转varchar或nvarchar,varchar或nvarchar转int的方法: ...
SQLSERVERAGENT ... 上的 SQLSERVERAGENT 服务启动过,然后又停止了。 (ObjectExplorer) 可...
SQL Server 中4个系... SQL Server 中4个系统数据库,Master、Model、Msdb、Tempdb 系统数据库...
SQL Server中如何设置... 对于已经建好的数据库表,是不能在SQL Server Management中可视化地修改ID为自增长...
该表已为了复制而被发布,所以... 场景:从发布库上将一数据库移到另一服务器,在对表改名时提示该表已为了复制而被发布,所以无法重命名。 ...
SQL Server 2008... SQL Server 2008 R2运行越久,占用内存会越来越大。 第一种: 有了上边的分析结果,解...
SQL Server (MSS... SQL Server (MSSQLSERVER) 启动后 自动生成文件 audittrace2022...
如果使用没有提供选项值的 Sq... 如果使用没有提供选项值的 SqlDependency,必须先调用 SqlDependency.Sta...
传递给数据库 'master'... 传递给数据库 master 中的日志扫描操作的日志扫描号无效 错误:连接数据库的时候提示:SQL S...
数据仓库SSAS+SSIS+... 数据仓库SSAS+SSIS+SSRS SSAS- 1,用ssas生成多维度,然后利用excel的da...