tab1
station_code  station_name
1001          北京
1002          天津
1003          上海
1004          重庆tab2
tic_oid   station_code 
12454        1001
12457        1001
15457        1004
45787        1002我现在要更新tab2的station_code,对应tab1的station_name,是批量更改,请问怎么更改

解决方案 »

  1.   

    update tab2 set station_code=b.station_name from tab2 a inner join tab1 b on a.station_code=b.station_code
      

  2.   

    请问一下,我在
    update tab2 set station_code=b.station_name from tab2 [添加where 条件]a inner join tab1 b on a.station_code=b.station_code
    [添加where 条件] 怎么提示语法错误
      

  3.   


    ---------------------------------
    --  Author: htl258(Tony)
    --  Date  : 2009-07-24 11:23:25
    ---------------------------------
    --> 生成测试数据表:tab1If not object_id('[tab1]') is null
    Drop table [tab1]
    Go
    Create table [tab1]([station_code] int,[station_name] nvarchar(2))
    Insert tab1
    Select 1001,'北京' union all
    Select 1002,'天津' union all
    Select 1003,'上海' union all
    Select 1004,'重庆'
    Go
    --Select * from tab1--> 生成测试数据表:tab2If not object_id('[tab2]') is null
    Drop table [tab2]
    Go
    Create table [tab2]([tic_oid] int,[station_code] nvarchar(4))
    Insert tab2
    Select 12454,1001 union all
    Select 12457,1001 union all
    Select 15457,1004 union all
    Select 45787,1002
    Go
    --Select * from tab2-->SQL查询如下:
    update tab2 set
        [station_code]=t.[station_name]
    from tab1 t
    where tab2.[station_code]=t.[station_code]select * from tab2
    /*
    tic_oid     station_code
    ----------- ------------
    12454       北京
    12457       北京
    15457       重庆
    45787       天津
    */
      

  4.   

    UPDATE TB2 SET  station_code =station_name FROM TB2 JOIN TB1 ON TB1.station_code=TB2.station_code楼主第二个station_code要是字符型才能更新的
    要不然直接查询出来做个视图
      

  5.   

    update tab2 set
        [station_code]=t.[station_name]
    from tab1 t
    where tab2.[station_code]=t.[station_code]
      and 你的条件这样也可以