我做了一下测试,全部都是错的,郁闷得很。这个是对的。declare   @bcpname   char(100)   
  select     @bcpname='bcp     factory..fac   in   f:\fac.txt     -c   -S   servername   -U   user   -P   password'   
  exec   master..xp_cmdshell   @bcpname  
我参照上面写的:declare @tablediffent char(1000)
set @tablediffent='tablediff -sourceserver "192.168.0.104\sql2005" -sourcedatabase "bobuitest" -sourcetable "StudentMessage" -sourceschema "dbo" -sourcepassword "123456" -sourceuser "sa" -destinationserver "192.168.0.8\s2005" -destinationdatabase "bobuitest" -destinationtable "StudentMessage" -destinationschema "dbo" -destinationpassword "sasa" -destinationuser "sa" -f'
exec master..xp_cmdshell @tablediffent
这样写的是错的。
请大家帮帮忙,谢谢大家了。

解决方案 »

  1.   

    USE tempdb
    GO--创建测试表
    CREATE TABLE TestTable
    (
     ID int PRIMARY KEY,
     Name varchar(max),
     Age tinyint
    )
    GO--插入测试数据
    INSERT TestTable
    SELECT 1,'John',24
    UNION ALL SELECT 3,'Mark',34
    UNION ALL SELECT 4,'David',51
    GO--创建临时比对表
    CREATE TABLE TestTable_Tmp
    (
     ID int PRIMARY KEY,
     Name varchar(max),
     Age tinyint
    )
    GO--用TableDiff工具比对
    EXEC xp_cmdshell 'tablediff -sourceserver "你的服务器名" -sourcedatabase "tempdb" -sourceschema "dbo" -sourcetable "TestTable" -sourceuser "sa" -sourcepassword "sa" -destinationserver "你的服务器名" -destinationdatabase "tempdb" -destinationschema "dbo" -destinationtable "TestTable_Tmp" -destinationuser "sa" -destinationpassword "sa" -f "F:\Test.sql"'
    GO--读取插入数据脚本
    DECLARE @tmp table(sqlstr varchar(max))
    INSERT @tmp
    EXEC('xp_cmdshell ''type F:\Test.sql''')
    SELECT 
     sqlstr 
    FROM @tmp
    WHERE sqlstr IS NOT NULL
     AND CHARINDEX('--',sqlstr) = 0
     
    --删除测试表和文件
    DROP TABLE TestTable_Tmp
    DROP TABLE TestTable
    GOEXEC xp_cmdshell 'del F:\Test.sql'
    GO
      

  2.   

    我电脑上没错,你的是错到那里
    declare   @bcpname   char(100)   
      select     @bcpname='bcp     factory..fac   in   f:\fac.txt     -c   -S   servername   -U   user   -P   password'   
      exec   master..xp_cmdshell   @bcpname  
    declare @tablediffent char(1000)
        set @tablediffent='tablediff -sourceserver "192.168.0.104\sql2005" -sourcedatabase "bobuitest" -sourcetable "StudentMessage" -sourceschema "dbo" -sourcepassword "123456" -sourceuser "sa" -destinationserver "192.168.0.8\s2005" -destinationdatabase "bobuitest" -destinationtable "StudentMessage" -destinationschema "dbo" -destinationpassword "sasa" -destinationuser "sa" -f'
        exec master..xp_cmdshell @tablediffent
      

  3.   

    IP改为链接服务器试试:
    --创建链接服务器 
    exec sp_addlinkedserver   'ITSV', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' 
    exec sp_addlinkedsrvlogin 'ITSV', 'false ',null, '用户名 ', '密码 ' 
    declare @tablediffent char(1000)
    set @tablediffent='tablediff -sourceserver "ITSV\sql2005" -sourcedatabase "bobuitest" -sourcetable "StudentMessage" -sourceschema "dbo" -sourcepassword "123456" -sourceuser "sa" -destinationserver "192.168.0.8\s2005" -destinationdatabase "bobuitest" -destinationtable "StudentMessage" -destinationschema "dbo" -destinationpassword "sasa" -destinationuser "sa" -f'
    exec master..xp_cmdshell @tablediffent
      

  4.   

    use master
    Go
    IF DB_ID('DatabaseA') IS NOT NULL   DROP DATABASE DatabaseA
    GO
    IF DB_ID('DatabaseB') IS NOT NULL   DROP DATABASE DatabaseB
    GO
    CREATE DATABASE DatabaseA
    GO
    CREATE DATABASE DatabaseB
    GO
    USE DatabaseA
    GO
    CREATE TABLE SourceTable
    (   IDCol INT IDENTITY(1,1),    Field1 SMALLINT,    Field2 SMALLINT,    Field3 SMALLINT,    Field4 SMALLINT 
    )
    GO
    USE DatabaseB
    GO
    CREATE TABLE SourceTable
    (   IDCol INT IDENTITY(1,1),    Field1 SMALLINT,    Field2 SMALLINT,    Field3 SMALLINT,    Field4 SMALLINT 
    )
    GO
    USE DatabaseA
    GO
    INSERT INTO SourceTable
    (Field1, Field2, Field3, Field4)
    SELECT 1, 1, 1, 2
    UNION
    SELECT 1, 1, 2, 2
    UNION
    SELECT 1, 3, 2, 2
    UNION
    SELECT 1, 3, 2, 2
    UNION
    SELECT 4, 3, 2, 2
    GO
    USE DatabaseB
    GO
    INSERT INTO SourceTable
    (Field1, Field2, Field3, Field4)
    SELECT 1, 1, 1, 2
    UNION
    SELECT 1, 3, 2, 1
    UNION
    SELECT 1, 3, 2, 2
    UNION
    SELECT 1, 3, 2, 2
    UNION
    SELECT 5, 3, 2, 2
    UNION
    SELECT 5, 4, 3, 2
    GO  批处理文件  在这个例子中,我将调用TableDiff utility,同时带上了一些必要的参数,对DatabaseA和DatabaseB两个数据库中各自的SourceTable表的数据进行比较。 Listing B中的脚本带着参数通过源服务器(SourceServer)、源数据库( SourceDatabase)、源表 SourceTable、目标服务器( DestinationServer)、目标数据库( DestinationDatabase)和目标表(DestinationTable)到达TableDiff utility。对于每一个站点来说,服务器名和表名都是相同的,因为我是在同一个数据库服务器的两个不同的数据库中比较使用的同一个名字的表。我通过的最后目的地是当地,同步脚本放置在那里。我将这个脚本存放在C:/根目录下,名字为diffs.txt。cd Program FilesMicrosoft SQL ServerCOMTableDiff -sourceserver "DatabaseServer" -sourcedatabase "DatabaseA" -sourcetable "SourceTable" -destinationserver "DatabaseServer" -destinationdatabase "DatabaseB" -destinationtable "SourceTable" -f "C:diffs.txt"
    PAUSE
      

  5.   

    BCP是固定格式吧!
    tablediff这个是什么鬼东西!
      

  6.   


    --try:
    declare @tablediffent char(1000)
        set @tablediffent='tablediff -sourceserver "[192.168.0.104\sql2005]" -sourcedatabase "bobuitest" -sourcetable "StudentMessage" -sourceschema "dbo" -sourcepassword "123456" -sourceuser "sa" -destinationserver "[192.168.0.8\s2005]" -destinationdatabase "bobuitest" -destinationtable "StudentMessage" -destinationschema "dbo" -destinationpassword "sasa" -destinationuser "sa" -f'
        exec master..xp_cmdshell @tablediffent
      

  7.   

    http://www.chinaz.com/Program/MSSQL/09191AR2007.html
      

  8.   

    http://blog.csdn.net/feixianxxx/archive/2009/07/21/4365576.aspx 参考看看吧
      

  9.   

    http://blog.csdn.net/feixianxxx/archive/2009/07/21/4365576.aspx
    连接 弄错了