网(LieHuo.Net)教程 首先要做的是安装sql server 2005 开发版或专业版,可以用两台机器,也可以在一台机器上装两个sql server instance(实例),我这里是采用的两台机器进行的,机器名分别为RENHU和CHINA-E931FACA9,在RENHU上装了sql server instance A,在CHINA-E931FACA9上装了B。安装的时候注意开启sql server和windows混合验证。
确保两台机器,通过机器名能够ping 通(在CHINA-E931FACA9上能ping通RENHU,在RENHU上能ping通CHINA-E931FACA9),否则修改host文件,确保两台机器能够ping通。
运行sql server Configureation Manager,使TCP/IP enable,如下图:
两台机器都要能要做,然后重启Sql server、Sql server Browser服务(这两个服务一定要起来),然后关闭防火墙,或设置防火墙例外。
然后试着用一台机器去连另外一台机器的sql server(用sql server manage studio),例如我这里用RENHU这台机器连CHINA-E931FACA9B,如果能连成功,则说明一切ok,如图:
配置ok,就要进行分布式配置了,代码如下:
Server RENHU:
以下为引用的内容: 1-- 建立数据库,设置数据的选项 2CREATE DATABASE SalesDB; 3EXECUTE sp_serveroption @server='RENHUA',@optname='lazy schema validation',@optvalue='true' 4CREATE LOGIN xqls WITH Password = 'wisdom317' 5 6GO 7 8-- 建立用户 9USE SalesDB 10CREATE USER xqls FROM LOGIN xqls 11 12GO 13 14-- 连接到B 15EXECUTE sp_addlinkedserver 'CHINA-E931FACA9B', 'SQL Server' 16EXEC sp_addlinkedsrvlogin 'CHINA-E931FACA9B','false',NULL,'xqls','wisdom317' 17 18GO 19 20-- 创建表结构 21IF OBJECT_ID('SalesHistory','U') > 0 22 23DROP TABLE SalesHistory 24 25GO 26 27CREATE TABLE SalesHistory 28 29( 30 SaleID INT PRIMARY KEY, 31 Product VARCHAR(30) NOT NULL, 32 SaleDate DATETIME, 33 SalePrice MONEY, 34 Region VARCHAR(5) NOT NULL, 35 CONSTRAINT chk_Region CHECK (SaleID <20000) 36 37) 38 39GO 40 41-- 添加测试数据 42DECLARE @i SMALLINT, @Region VARCHAR(5) 43 44SET @i = 1 45 46SET @Region = 'West' 47 48 49 50WHILE (@i <=6000) 51 52BEGIN 53 INSERT INTO SalesHistory 54 (SaleID, Product, SaleDate, SalePrice, Region) 55 VALUES 56 57(@i, 'Computer', DATEADD(mm, @i, '3/11/1919'), DATEPART(ms, GETDATE()) + (@i + 57), @Region ) 58 SET @i = @i + 1 59 INSERT INTO SalesHistory 60 (SaleID, Product, SaleDate, SalePrice, Region) 61 VALUES 62 (@i, 'BigScreen', DATEADD(mm, @i, '3/11/1927'), DATEPART(ms, GETDATE()) + (@i + 13), @Region) 63 SET @i = @i + 1 64 INSERT INTO SalesHistory 65 (SaleID, Product, SaleDate, SalePrice, Region) 66 VALUES 67 (@i, 'PoolTable', DATEADD(mm, @i, '3/11/1908'), DATEPART(ms, GETDATE()) + (@i + 29), @Region ) 68 SET @i = @i + 1 69 70END 71 72GO 73 74-- 分配权限 75GRANT SELECT, INSERT, UPDATE, DELETE, VIEW DEFINITION ON SalesHistory TO xqls 76 77GO 78 79-- 建立视图 80CREATE VIEW dpv_SalesHistory 81 82AS 83 SELECT 84 SaleID, Product, Region, SaleDate, SalePrice 85 FROM SalesDB.dbo.SalesHistory 86 UNION ALL 87 SELECT 88 SaleID, Product, Region, SaleDate, SalePrice 89 FROM [CHINA-E931FACA9B].SalesDB.dbo.SalesHistory 90 91GO 92 93-- 测试视图 94select * from dpv_SalesHistory |
Server CHINA-E931FACA9:
以下为引用的内容: 1CREATE DATABASE SalesDB; 2EXECUTE sp_serveroption @server='CHINA-E931FACA9B',@optname='lazy schema validation',@optvalue='true' 3CREATE LOGIN xqls WITH Password = 'wisdom317' 4 5GO 6 7USE SalesDB 8CREATE USER xqls FROM LOGIN xqls 9 10GO 11 12-- 连接到A 13EXECUTE sp_addlinkedserver 'RENHUA', 'SQL Server' 14EXEC sp_addlinkedsrvlogin 'RENHUA','false',NULL,'xqls','wisdom317' 15 16GO 17 18-- 创建表结构 19IF OBJECT_ID('SalesHistory','U') > 0 20 21DROP TABLE SalesHistory 22 23GO 24 25CREATE TABLE SalesHistory 26 27( 28 SaleID INT PRIMARY KEY, 29 Product VARCHAR(30) NOT NULL, 30 SaleDate DATETIME, 31 SalePrice MONEY, 32 Region VARCHAR(5) NOT NULL, 33 CONSTRAINT chk_Region CHECK (SaleID >=20000) 34 35) 36 37GO 38 39 40-- 添加测试数据 41DECLARE @i SMALLINT, @Region VARCHAR(5) 42 43SET @i = 20000 44 45SET @Region = 'East' 46 47 48 49WHILE (@i <=26000) 50 51BEGIN 52 INSERT INTO SalesHistory 53 (SaleID, Product, SaleDate, SalePrice, Region) 54 VALUES 55 (@i, 'Computer', DATEADD(mm, @i, '3/11/1919'), DATEPART(ms, GETDATE()) + (@i + 57), @Region ) 56 SET @i = @i + 1 57 INSERT INTO SalesHistory 58 (SaleID, Product, SaleDate, SalePrice, Region) 59 VALUES 60 (@i, 'BigScreen', DATEADD(mm, @i, '3/11/1927'), DATEPART(ms, GETDATE()) + (@i + 13), @Region) 61 SET @i = @i + 1 62 INSERT INTO SalesHistory 63 (SaleID, Product, SaleDate, SalePrice, Region) 64 VALUES 65 (@i, 'PoolTable', DATEADD(mm, @i, '3/11/1908'), DATEPART(ms, GETDATE()) + (@i + 29), @Region ) 66 SET @i = @i + 1 67 68END 69 70-- 分配用户权限 71GRANT SELECT, INSERT, UPDATE, DELETE, VIEW DEFINITION ON SalesHistory TO xqls 72 73GO 74 75-- 建立视图 76CREATE VIEW dpv_SalesHistory 77 78AS 79 SELECT 80 SaleID, Product, Region, SaleDate, SalePrice 81 FROM SalesDB.dbo.SalesHistory 82 UNION ALL 83 SELECT 84 SaleID, Product, Region, SaleDate, SalePrice 85 FROM [RENHUA].SalesDB.dbo.SalesHistory 86 87GO 88 89-- 测试视图 90select * from dpv_SalesHistory |