用存储过程实现SQL Server数据库同步

数据库 SQL Server
本文主要介绍了使用SQL Server数据库的存储过程来实现数据库同步的方法,通过下面的代码让我们来了解一下这一过程的实现。

下面我们开始介绍用存储过程实现SQL Server数据库的同步方法,首先说明,我们使用的存储过程是需要运行在服务器上的,如果换库,不需要修改任何东西。接下来我们就逐步演示这一过程:

建立存储所有表名的表:

 

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].p_bakup_tatle_all') and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
  2.  
  3. drop procedure [dbo].p_bakup_tatle_all  
  4.  
  5. GO  
  6.  
  7. create proc p_bakup_tatle_all  
  8.  
  9. as  
  10.  
  11. if exists (select * from dbo.sysobjects where id = object_id(N'table_all ') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  12.  
  13. drop table table_all  
  14.  
  15. CREATE TABLE [dbo].[Table_all](  
  16.  
  17.     [id] [int] IDENTITY(1,1) NOT NULL,  
  18.  
  19.     [name] [varchar](50) COLLATE Chinese_PRC_CI_AS NULL,  
  20.  
  21.  CONSTRAINT [PK_Table_all] PRIMARY KEY CLUSTERED  
  22.  
  23. (  
  24.  
  25.     [id] ASC  
  26.  
  27. )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]  
  28.  
  29. ) ON [PRIMARY] 

 

将所有表名存放在表table_all中:

 

  1. insert  into table_all(name) select name from sysobjects where xtype='U' 
  2.  
  3. GO 

 

备份服务器上的存储过程,若换库,需要修改InfoCenter两处,还有连接服务的地址。

创建数据同步的存储过程:

 

  1. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].p_bakup_tatle_all') and OBJECTPROPERTY(id, N'IsProcedure') =  
  2.  
  3. 1)  
  4.  
  5.  drop procedure [dbo].p_bakup_tatle_all  
  6.  
  7. GO  
  8.  
  9. create proc p_bakup_tatle_all  
  10.  
  11. as 

 

创建链接服务器:

 

  1. exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '61.135.203.103'  
  2.  
  3. exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, 'sa', 'comsky103@2011.com' 

 

查询示例 select * from ITSV.test.dbo.[users]。

导入将服务器上的表以及数据保存在本地:

 

  1. if exists (select * from dbo.sysobjects where id = object_id(N'table_all ') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  2.  
  3.  drop table table_all  
  4.  
  5. select * into table_all  from ITSV.InfoCenter.dbo.table_all  
  6.  
  7. DECLARE @Name varchar(50),@count int,@i int  
  8.  
  9. set @i=1 
  10.  
  11. select @countcount=count(*) from table_all  
  12.  
  13. while @i<=@count  
  14.  
  15. begin  
  16.  
  17. select @Name=name from table_all where id=@i  
  18.  
  19. if  exists(select name from sysobjects where name=''+@name+'' and type='u')     
  20.  
  21. exec('drop table ['+@Name+']')  
  22.  
  23. exec('select * into ['+@Name+'] from ITSV.InfoCenter.dbo.['+@Name+']' )  
  24.  
  25. PRINT @Name  
  26.  
  27. set @i=@i+1  
  28.  
  29. PRINT @i 

 

以后不再使用时删除链接服务器:

 

  1. exec sp_dropserver 'ITSV ', 'droplogins'  
  2.  
  3. go 

 

 以上就是用存储过程实现SQL Server数据库同步的全部过程,本文就介绍到这里,谢谢大家的支持!

【编辑推荐】

  1. 巧用DAC解决SQL Server登录失败的问题
  2. 如何将系统监视器数据记录到SQL Server
  3. 使用SQL Trace来实现SQL Server的跟踪操作
  4. CTE和WITH AS短语结合使用提高SQL查询性能
  5. 简述SQL Server Replication的常见错误及其处理
责任编辑:赵鹏 来源: 博客园
相关推荐

2010-08-27 09:59:51

SQL Server

2010-09-06 11:05:05

SQL SERVER语句

2011-08-29 10:55:03

SQL Server分页存储过程优化效率分

2011-07-28 14:31:47

SQL Server数存储过程

2011-08-11 09:49:33

SQL Server 存储过程插入更新数据

2011-08-25 17:15:04

2011-09-01 14:00:11

SQL Server 存储过程显示表结构

2010-07-22 11:17:52

SQL Server数

2011-09-07 15:11:31

SQL Server同步

2011-07-19 15:18:46

存储过程sql语句

2010-06-28 13:45:16

SQL Server

2011-08-15 15:14:54

SQL Server存储过程异常处理

2010-07-15 17:28:50

SQL Server

2011-08-10 10:06:54

存储过程SQL Server IP地址归属地查询

2009-08-12 11:04:38

ASP.NET和SQL

2010-10-20 16:17:17

SQL Server角

2010-07-01 15:44:22

SQL Server数

2010-06-30 16:48:19

SQL Server数

2010-06-30 14:24:08

SQL Server数

2009-08-06 16:44:06

点赞
收藏

51CTO技术栈公众号