SQL新舊資料庫結構一致,所有表資料的比較SQL

2021-05-27 11:43:39 字數 2583 閱讀 2103

if object_id('tempdb..#temptable') is not null      

drop table #temptable

select distinct

a.table_name

,a.table_catalog+'.'+a.table_schema+'.' as sv105

,'[192.168.1.130].'+a.table_catalog+'.'+a.table_schema+'.' as sv130

,a.column_name

,a.is_nullable

into #temptable

from information_schema.columns as a

inner join information_schema.tables as b

on a.table_catalog = b.table_catalog

and a.table_schema = b.table_schema

and a.table_name = b.table_name

where

b. table_type='base table'

declare @tname as varchar(100)

declare @sv1 as varchar(100)

declare @sv2 as varchar(100)

select * from #temptable

while exists(select 1 from #temptable)

begin

declare @cname as varchar(200)

declare @isnullable as varchar(100)

select top 1

@tname = table_name

,@sv1 =sv105

,@sv2 =sv130

from #temptable

declare @sql as varchar(8000)

set @sql=

' select '''+@tname+''' as table_name,count(1) as rows from ' + @sv1+@tname + char(10)+

' select '''+@tname+''' as table_name,count(1) as rows from ' + @sv2+@tname + char(10)+

' select '''+@tname+''' as table_name,count(1) as rows from ' + @sv1+@tname + ' as a ' + char(10) +

' inner join ' +@sv2+@tname + ' as b ' + char(10)

declare abc cursor for

select column_name,is_nullable

from #temptable

where table_name =@tname

open abc

fetch next from abc into @cname,@isnullable

declare @index as int

set @index = 0

while @@fetch_status = 0

begin

set @index =@index+1

set @sql = @sql +

case when @index =1 then ' on '

else ' and '

end

+case when @isnullable='yes' then 'isnull(a.'+@cname+',0)'

else ' a.'+@cname

end

+' = '

+case when @isnullable='yes' then 'isnull(b.'+@cname+',0)'

else ' b.'+@cname

end

+ char(10)

fetch next from abc into @cname,@isnullable

end

close abc

deallocate abc

print @sql

delete from #temptable where table_name =@tname

end

sql語句比較新舊兩個表資料中資料的一致性

select a.callingcode,a.calledcode,a.calldate,a.calltime,a.billtype,a.money,b.callingcode,b.calledcode,b.calldate,b.calltime,b.billtype,b.money from bi...

資料庫一致性

資料庫一致性 database consistency 是指事務執行的結果必須是使資料庫從乙個一致性狀態變到另乙個一致性狀態。保證資料庫一致性是指當事務完成時,必須使所有資料都具有一致的狀態。在關係型資料庫中,所有的規則必須應用到事務的修改上,以便維護所有資料的完整性。保證資料庫的一致性是資料庫管理...

快取和資料庫一致性

對應比較常用的資料,比如鑑權資料一般會放在快取中 比如 redis 這樣能夠跟快的實現讀取,所以一般讀取流程如下 目前網上有很多關於快取和資料庫怎麼保持一致性的文件,主要可以總結為如下幾點 1 先更新資料庫,在更新快取 2 先刪除快取,在更新資料庫 3 先更新資料庫,在刪除快取 下面將對著幾種機制作...