用Tablediff把表數據生成SQL腳本

use tempdb
go
--表test
if object_id('test') is not null
    drop table test
create table Test(ID int identity(1,1) constraint PK_Test primary key,Name nvarchar(100) constraint U_Test_Name unique,Memo nvarchar(max),CreateDate datetime)
insert Test select 'TestName1','備注1','2008-01-15 12:00'
insert Test select 'TestName2','備注2','2008-01-15 13:00'
go
--創建臨時表對比
create table Tmp_Test(ID int identity(1,1) primary key,Name nvarchar(100) unique,Memo nvarchar(max),CreateDate datetime)
go
查看用法:

http://technet.microsoft.com/zh-cn/library/ms162843.aspx

--在命令執行
--運行——cmd——"cd C:/Program Files/Microsoft SQL Server/90/COM" --指定tablediff.exe文件路徑
--實例名:Roy
--執行以下命令:
--tablediff -sourceserver "Roy" -sourcedatabase "tempdb" -sourceschema "dbo" -sourcetable "test" -sourceuser "sa" -sourcepassword "sa密碼" -destinationserver "Roy" -destinationdatabase "tempdb" -destinationschema "dbo" -destinationtable "Tmp_Test" -destinationuser "sa" -destinationpassword "sa密碼" -f "E:/TestData.sql" -o "E:/Testlog.txt"
go

--查看TestData

/*

-- Host: Roy
-- Database: [tempdb]
-- Table: [dbo].[Tmp_Test]
-- Column(s) Memo are not included in this script because they are of type(s) text, ntext, varchar(max), nvarchar(max), varbinary(max), image, timestamp, or xml. Columns of these types cannot be updated by tablediff utility scripts; therefore non-convergence of data can still occur after this script has been applied. If the tablediff utility output references any of these columns, you must update the columns manually if you want them to converge.
SET IDENTITY_INSERT [dbo].[Tmp_Test] ON
INSERT INTO [dbo].[Tmp_Test] ([CreateDate],[ID],[Name]) VALUES ('2008-01-15 12:00:00.000',1,'TestName1')
INSERT INTO [dbo].[Tmp_Test] ([CreateDate],[ID],[Name]) VALUES ('2008-01-15 13:00:00.000',2,'TestName2')
SET IDENTITY_INSERT [dbo].[Tmp_Test] OFF

*/

--查看Testlog
/*
Table [tempdb].[dbo].[test] on Roy and Table [tempdb].[dbo].[Tmp_Test] on Roy have 2 differences.
Fix SQL written to E:/TestData.sql.
Err    ID    Col
Src. Only    1   
Src. Only    2   
The requested operation took 0.2187038 seconds.

*/


--在查詢分析里查看
DECLARE @Test table(SQL varchar(max))
INSERT @Test EXEC xp_cmdshell 'Type E:/TestData.sql'
SELECT  SQL FROM @Test WHERE SQL IS NOT NULL AND SQL not like N'--%' and  SQL not like 'nce%'

--處理后的生成格式

/*
SET IDENTITY_INSERT [dbo].[Tmp_Test] ON
INSERT INTO [dbo].[Tmp_Test] ([CreateDate],[ID],[Name]) VALUES ('2008-01-15 12:00:00.000',1,'TestName1')
INSERT INTO [dbo].[Tmp_Test] ([CreateDate],[ID],[Name]) VALUES ('2008-01-15 13:00:00.000',2,'TestName2')
SET IDENTITY_INSERT [dbo].[Tmp_Test] OFF
*/

drop table Test,Tmp_Test

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值