直接用数据导入导出工具DTS来做吧

解决方案 »

  1.   

    例如:数据库中的内容 
    x    name    sex    id
    1     张     男     928
    2     李     男     736
    3     钱     男     436
    4     石     男     092
    5     人     男     186
    文本的内容
    x    name    sex    id
    1     技     男     452
    2     李     男     736
    3     何     男     903
    如何正确导入数据?
      

  2.   

    insert into people
    select f1+4,f2,f3,f3 from 
    OPENROWSET('MICROSOFT.JET.OLEDB.4.0'
    ,'Text;HDR=NO;DATABASE=C:\'
    ,aa#txt) where f4 not in(select id from people)
      

  3.   

    我插入数据都是通过存储过程 x=count(*)+1的方法
    删除记录时通过触发器 update people set x=x-1 where x>y  --y是删除记录的x值
    如果在中间有几条记录重复了,x字段就会出错
      

  4.   

    --下面是数据处理
    --先导入临时表
    create table ##t(x int,name varchar(10),sex varchar(10),id varchar(10))
    exec master..xp_cmdshell 'bcp "##t" in "c:\b.txt" /c /S"服务器名" /U"用户名" /P"密码"',no_outputdeclare @x int
    select @x=max(x) from 原表
    if @x is null set @x=0--删除临时表中与原表重复的数据
    delete ##t from ##t a join 原表 b on a.id=b.id--处理编号
    update ##t set @x=@x+1,x=@x from ##t--导入原表
    insert into 原表 select * from ##t--显示导入结果
    select * from 原表--完成后删除临时表
    drop table ##t
      

  5.   

    --下面是数据测试--创建测试表
    create table 原表(x int,name varchar(10),sex varchar(10),id varchar(10))
    insert into 原表
    select 1,'张','男','928'
    union all select 2,'李','男','736'
    union all select 3,'钱','男','436'
    union all select 4,'石','男','092'
    union all select 5,'人','男','186'go
    --下面是数据处理
    --先导入临时表
    create table ##t(x int,name varchar(10),sex varchar(10),id varchar(10))
    exec master..xp_cmdshell 'bcp "##t" in "c:\b.txt" /c /S"zj" /P""',no_outputdeclare @x int
    select @x=max(x) from 原表
    if @x is null set @x=0--删除临时表中与原表重复的数据
    delete ##t from ##t a join 原表 b on a.id=b.id--处理编号
    update ##t set @x=@x+1,x=@x from ##t--导入原表
    insert into 原表 select * from ##t--显示导入结果
    select * from 原表--完成后删除临时表
    drop table ##tgo
    --删除数据测试环境
    drop table 原表/*--测试结果
    x           name       sex        id         
    ----------- ---------- ---------- ---------- 
    1           张          男          928
    2           李          男          736
    3           钱          男          436
    4           石          男          092
    5           人          男          186
    6           技          男          452
    7           何          男          903(所影响的行数为 7 行)--*/
      

  6.   

    假如我的文本路径是:c:\people.txt
    请各位大虾帮帮忙
      

  7.   

    可是可以了,不过速度好慢,7000条记录中只有一条记录插入就用了10秒钟
    我的CPU是2.4G的,还不知道客户的机器是什么
    有没有方法可以不用临时表的啊?
      

  8.   

    如果你的文本文件中的记录是用,分隔的,就可以直接用openrowset.不过,速度应该不会那么慢吧?
      

  9.   

    不是,文本文件的记录是用master..xp_cmdshell导出的
    好象是以一个 tab 分隔的
      

  10.   

    --试试下面这种方法的速度
    --先导入临时表
    create table ##t(x int,name varchar(10),sex varchar(10),id varchar(10))
    exec master..xp_cmdshell 'bcp "##t" in "c:\b.txt" /c /S"zj" /P""',no_outputdeclare @x int
    select @x=max(x) from 原表
    if @x is null set @x=0--处理编号
    update ##t set @x=@x+1,x=@x
    from ##t a left join 原表 b on a.id=b.id
    where b.id is null--导入原表
    insert into 原表 
    select a.* from ##t a left join 原表 b on a.id=b.id
    where b.id is null--显示导入结果
    select * from 原表--完成后删除临时表
    drop table ##t
      

  11.   

    不行啊
    就exec master..xp_cmdshell 'bcp "##t" in "c:\b.txt" /c /S"zj" /P""',no_output
    一条语句执行的时间就6秒