同步資料庫資訊(儲存過程 游標)

2021-09-26 07:20:52 字數 4205 閱讀 2907

專案基本都是基於現在已有的資料庫進行開發,或者擴充套件,基本碰到的專案都是2~3個以上的資料庫,頭疼。一般做的比較基礎或者常用的就是寫下同步,比如員工,部門,角色,等等的一些同步,

我的操作是直接建立儲存過程,然後去設定sqlserver的定時任務,定時去執行這個儲存過程,那麼就實現了我的需求。

--建立儲存過程

create procedure [dbo].[pro_basicdatatransfer]

asbegin

declare @f_companyid nvarchar(50)='8d168d52-23f1-4743-b8b2-7f256add9b97' --公司

--使用者

insert into t_base_user(f_userid, f_encode, f_account, f_password, f_secretkey, f_realname, f_mobile,

f_birthday, f_gender, f_departmentid, f_companyid, f_deletemark, f_enabledmark, f_createuserid, f_createusername, f_createdate)

select e.fitemid, e.fmobilephone ,e.fmobilephone, '3caa63ad3aef84c83fad7a0f502d8be1', '5ecccf68c782d90e', e.fname, e.fmobilephone

, e.fbirthday, case isnull(e.fgender, 0) when 1068 then 1 else 0 end , e.fdepartmentid, @f_companyid, 0, 1, 'system', 'system', getdate()

from ais2019..t_emp e

where not exists( select 1 from t_base_user u where cast(e.fitemid as varchar) = u.f_userid )

--宣告游標

declare mycursorq cursor

for select e1.fitemid ,e1.fmobilephone, e1.fname,

e1.fmobilephone, e1.fbirthday, case isnull(e1.fgender, 0) when 1068 then 1 else 0 end as fgender, e1.fdepartmentid from ais2019..t_emp e1

where not exists( select 1 from t_base_user u where cast(e1.fitemid as varchar(50)) = u.f_userid and

f_companyid = @f_companyid and

isnull(e1.fmobilephone, '0') = isnull(u.f_mobile, '0') and

isnull(e1.fdepartmentid, '0') = isnull(u.f_departmentid, '0') and

isnull(e1.fname, '0') = isnull(u.f_realname, '0') and

isnull(e1.fbirthday, getdate()) = isnull(u.f_birthday, getdate()) and

case isnull(e1.fgender, '0') when 1068 then 1 else 0 end = isnull(u.f_gender, '0') and

isnull(e1.fshortnumber, '0') = isnull(u.f_account, '0')

) --開啟乙個游標

open mycursorq

--迴圈乙個游標

declare @fitemid nvarchar(2000), @fshortnumber nvarchar(2000), @fname nvarchar(2000), @fmobile nvarchar(2000), @fbirthday nvarchar(2000),

@fgender nvarchar(2000), @fdeptno nvarchar(2000)

--移動游標指向到第一條資料,提取第一條資料存放在變數中 ,

fetch next from mycursorq into @fitemid, @fshortnumber, @fname, @fmobile, @fbirthday, @fgender, @fdeptno

--如果上一次操作成功則繼續迴圈

while @@fetch_status =0

begin

update t_base_user set f_mobile=@fmobile, f_departmentid=@fdeptno, f_realname=@fname,

f_birthday=@fbirthday, f_gender=@fgender, f_account=@fshortnumber , f_encode=@fshortnumber where f_userid=@fitemid

and f_companyid = @f_companyid

fetch next from mycursorq into @fitemid, @fshortnumber, @fname, @fmobile, @fbirthday, @fgender, @fdeptno

end

close mycursorq

deallocate mycursorq

-- 部門

insert into t_base_department(f_departmentid, f_companyid, f_encode, f_fullname, f_sortcode,

f_deletemark, f_enabledmark, f_createdate, f_createuserid, f_createusername,f_parentid)

select fitemid, @f_companyid, fnumber, fname, fitemid,

0, 1, getdate(), 'system', 'system',0 from ais2019..t_department d

where not exists(select 1 from t_base_department b where cast(d.fitemid as varchar) = b.f_departmentid )

--宣告游標

declare mycursorw cursor

for select fitemid, fnumber, fname from ais2019..t_department d

where exists( select 1 from t_base_department b where b.f_departmentid = cast(d.fitemid as varchar) and

f_companyid = @f_companyid and

fitemid <> b.f_departmentid or

fnumber <>b.f_encode or

fname <>b.f_fullname)

--開啟乙個游標

open mycursorw

--迴圈乙個游標

declare @fitemid1 nvarchar(2000), @fnumber1 nvarchar(2000), @fname1 nvarchar(2000)

--移動游標指向到第一條資料,提取第一條資料存放在變數中

fetch next from mycursorw into @fitemid1, @fnumber1, @fname1

--如果上一次操作成功則繼續迴圈

while @@fetch_status =0

begin

update t_base_department set f_encode=@fnumber1, f_fullname=@fname1

where f_departmentid=@fitemid1

fetch next from mycursorw into @fitemid1, @fnumber1, @fname1

end

close mycursorw

deallocate mycursorw

end

儲存過程 資料庫主從同步

儲存過程 為完成某一功能的一系列的sql語句集合。乙個儲存過程是乙個可程式設計的函式,它在資料庫中建立並儲存。它可以有sql語句和一些特殊的控制結構組成。儲存過程具有的優點如下 1,增加sql語句的靈活性跟功能性。儲存過程被建立後,可以在程式中被多次呼叫,而不必重新編寫該儲存過程的sql語句。2,加...

Oracle資料庫儲存過程 使用游標(2)

第一種使用loop 迴圈 loop fetch c postype into v postype,v description exit when c postype notfound end loop 備註 exit when語句一定要緊跟在fetch之後。必避免多餘的資料處理。而處理邏輯需要跟在e...

MySQL儲存過程跨資料庫操作同步資料

begin declare deliverstatus,smlen,datacoding,total int set total 0 while total 20 do set tsid if tsid then if resulttime and recvtime then elseif resu...