不明白楼主什么意思.我的理解是:
insert into tableName(field1)
select field1
from ....
其中field1是主键,但检索出来的结果field1不唯一。那么你可以用select distinct替换select。

解决方案 »

  1.   

    这样的话,可以用异常处理来实现呀,如果是关键字重覆了,就不再insert呀
      

  2.   

    关键字相同是什么意思?
    insert只插入第一条:insert into table2
    select top 1 * 
    from table1 
    where ...
      

  3.   

    举例说明一下:
    table结构如下:(education)
    wno,major,schoolname,begindate,enddate,
    其中wno,major & begindate为关键字
    现在我需要把一临时table中的4条记录插入到education中
    wno        major      schoolname begindate enddate
    '0209402','computer','苏州大学',19970901,  20010701
    '0209402','computer','东北大学',19970901,  20020701
    '0209402','english', '东北大学',20010901,   20030701
    '0209407','chinese', '东北大学',20010901,   20030701
    其中1,2的关键字相同,不知如何处理.to 这样的话,可以用异常处理来实现呀,如果是关键字重覆了,就不再insert
    如何实现啊.
      

  4.   

    to  zjcxc(邹建)
    您的意思是在存储过程中用游标定位纪录,然后逐个插入吗?有没有办法一条sql语句实现?
    关注中...
      

  5.   

    如果在存储过程中直接插入就可以了,关键字重覆了,SQL Server会自动会滚当前语句,后面的继续执行。
    在前台的话需要加入错误异常处理
      

  6.   

    to lssp:
    游标很简单,还是使用游标吧。用游标出错处理如何写了,因为现在它还是会包错啊
      

  7.   

    出错处理:当出错时 rollback tran 回滚
      

  8.   

    Declare @wno nchar(50),@type integer,@proficiency nchar(20)
      
      Declare cur cursor
           for select distinct * from #f
           for read only 
         
      open cur  fetch next from cur into @wno,@type,@proficiency
    while @@error=0

    begin
        Insert Into bqp..language (wno , type , proficiency) Values (@wno , @type , @proficiency)
    end 
    if (@@error<>0)
      rollback tran

    FETCH NEXT FROM cur
    into @wno,@type,@proficiency  close cur
      deallocate cur以上是我写的游标
      

  9.   

    Declare @wno nchar(50),@type integer,@proficiency nchar(20)
      Declare cur cursor
           for select distinct [Employee Number],[Language Type],Proficiency from [Speciality$]  
      open cur
    --  on error resume next
    fetch next from cur into @wno,@type,@proficiency
    begin tran
      while @@FETCH_STATUS=0   begin
           Insert Into bqp..language (wno , type , proficiency) Values (@wno , @type , @proficiency)
           
           FETCH NEXT FROM cur into @wno,@type,@proficiency
           print @@error
           print @@rowcount
           IF @@error != 0 OR @@rowcount != 1
           begin
             print  @@error
             rollback tran
     --close cur
     --DEALLOCATE curselect *
             --return 
           end
       endclose cur
    commit tran
    DEALLOCATE cur已经搞定了,你们给答案真是模糊啊