更新语句在sql中失败

时间:2016-08-16 20:12:41

标签: sql sql-server sql-update

以下更新声明给我错误,请在我出错的地方纠正我

  

Msg 102,Level 15,State 1,Line 1
  我

附近的语法不正确      

Msg 102,Level 15,State 1,Line 20
  你附近的语法不正确

代码:

Update [dbo].[ManPlan_Prescriber_Roster] i  
set i.territory_no = u.territory_no,  
    i.key_sales_force = u.key_sales_force,  
    i.territory_type_id = u.territory_type_id,  
    i.territory_type_descr = u.territory_type_descr,  
    i.last_name = u.last_name,  
    i.first_name = u.first_name,  
    i.mi = u.mi,  
    i.territory_orig_assign_dt = u.territory_orig_assign_dt,  
    i.territory_strt_dt = u.territory_strt_dt,  
    i.email_address = u.email_address,  
    i.cell_phone = u.cell_phone,  
    i.fcl_addr1 = u.fcl_addr1,  
    i.fcl_city = u.fcl_city,  
    i.fcl_st = u.fcl_st,  
    i.fcl_zip = u.fcl_zip  
from 
    [dbo].[ManPlan_Prescriber_Roster] m  
join 
    (select 
         territory_no,  
         key_sales_force,  
         territory_type_id,  
         territory_type_descr,  
         last_name,  
         first_name,  
         mi,  
         territory_orig_assign_dt,  
         territory_strt_dt,  
         email_address,  
         cell_phone,  
         fcl_addr1,  
         fcl_city,  
         fcl_st,  
         fcl_zip 
     from 
         [dbo].[RepRosterUpd$]  
     where 
         filedt = '2016-05-18') u on m.territory_no = u.territory_no 
                                  and m.key_sales_force = u.key_sales_force
                                  and m.territory_type_id = u.territory_type_id

由于

1 个答案:

答案 0 :(得分:0)

如果您使用要在from子句中更新的表格并在其中添加别名(此处为m),那么您必须使用该别名您的UPDATE声明:UPDATE m SET ..... - 您无法在UPDATE声明顶部使用其他别名“重新定义”该表

试试这个:

Update m              // use the alias you've defined on the FROM line!
set m.territory_no = u.territory_no,  
    m.key_sales_force = u.key_sales_force,  
    ......
    m.fcl_zip = u.fcl_zip  
from 
    [dbo].[ManPlan_Prescriber_Roster] m   // use this alias in UPDATE 
join 
    (select 
         territory_no,  
         key_sales_force,  
         .....
         fcl_zip 
     from 
         [dbo].[RepRosterUpd$]  
     where 
         filedt = '2016-05-18') u on m.territory_no = u.territory_no 
                                  and m.key_sales_force = u.key_sales_force
                                  and m.territory_type_id = u.territory_type_id