[code=BatchFile]@echo off
echo _______BEGIN______>>test.txtbcp "fee.dbo.Slip" in "Slip.dat" -S. -Usa -P  -n  -E if errorlevel 1 goto :errorgoto :end:error
date /T >>test.txt
time /T >>test.txt
there is an error >>test.txt
goto:end:end
echo _______END______>>test.txt[/code] 
在BCP IN 出现主键重复这个错误时,怎么才能让能让批处理跳到:error这里???
注:
1.无论BCP IN成功或失败 errorlevel 都是0(echo %errorlevel%)
2.我试过在BCP IN 加 -e 参数(-e errfile.dat),然后用IF EXIST errfile.dat goto :error 但是每次执行BCP IN都生成errfile.dat (很让我失望)

解决方案 »

  1.   

    --换一个方式:--导入表T主键col1insert T
    select

    from
     OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};
    DefaultDir=E:\;','select * from roy.txt')a
    where
    not exists(select 1 from T where col1=a.Col1 )
      

  2.   

    先将数据导入到另一个同构无PK的临时表中。
    在该表中手工处理,剔除故障数据后,insert into 目标表