Synchronize database information (stored procedure + cursor)

Posted by xtopolis on Sat, 05 Oct 2019 11:07:21 +0200

The project is basically based on the existing database for development, or expansion, basically encounter projects are more than 2 to 3 databases, headache. The basic or common way to do this is to write down the synchronization, such as the synchronization of employees, departments, roles, etc.

My operation is to create a stored procedure directly, and then set SqlServer's timed task to execute the stored procedure regularly, then I realized my requirements.

--Create stored procedures
ALTER procedure [dbo].[pro_BasicDataTransfer]
as

begin
	
	DECLARE @F_CompanyId nvarchar(50)='8d168d52-23f1-4743-b8b2-7f256add9b97' --company
--user 	
	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 cursor   
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 a cursor 
OPEN MyCursorQ
--Loop a cursor 
DECLARE @FItemID nvarchar(2000), @FShortNumber nvarchar(2000), @FName nvarchar(2000), @FMobile nvarchar(2000), @FBirthday nvarchar(2000), 
@FGender nvarchar(2000), @FDeptNo nvarchar(2000)

--Moving cursor points to the first data, extracting the first data and storing it in variables , 
FETCH NEXT FROM MyCursorQ INTO @FItemID, @FShortNumber, @FName, @FMobile, @FBirthday, @FGender,  @FDeptNo
--Continue the loop if the last operation succeeded 	
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 


-- department 
	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 cursor   
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 a cursor 
OPEN MyCursorW

--Loop a cursor 
DECLARE @FItemID1 nvarchar(2000), @FNumber1 nvarchar(2000), @FName1 nvarchar(2000)
--Moving cursor points to the first data, extracting the first data and storing it in variables 
FETCH NEXT FROM MyCursorW INTO @FItemID1, @FNumber1, @FName1
--Continue the loop if the last operation succeeded 	
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
 

 

Topics: Stored Procedure Database