这是我写的视图中的列 :AgentID, AgentName,ManageRegionCode, ManageRegionID,RegionName ,RegionId,Region, LargeRegionId, LargeRegion
我需要将AgentID,ManageRegionCode两列中的数据导到表B中,如果表B中存在该条记录则不保存,表B中已经有一些数据,经理要求写一个存储过程,参数有@AgentId, @ManageRegionId两个,麻烦大家帮忙想想该怎么写吧,小弟真的很着急

解决方案 »

  1.   


    create procedure pr_test(
    @AgentId int, 
    @ManageRegionId int
    )
    as
    begin
     
     if not exists (select 1 from B where AgentID = @AgentID and ManageRegionId=@ManageRegionId)
     begin
    insert into B
    select * from A where AgentID = @AgentID and ManageRegionId=@ManageRegionId
     end
            
    end
      

  2.   


    --不用存储过程一句话搞定insert into b(AgentID,ManageRegionCode)
    select AgentID,ManageRegionCode from A a
    where not exists (select 1 from B b where a.AgentID = b.AgentID and a.ManageRegionId = b.ManageRegionId )
      

  3.   

    这个“select 1 from B where AgentID = @AgentID and ManageRegionId=@ManageRegionId”中的1是什么意思?可以换成*号吗?