有一个TEST表
s_id   s_aa         s_ab
1 AA-AA-001    AB-AA-001
2 AA-AB-001    AB-AA-002
3 AA-AC-001    AB-AS-001
4 AA-AD-001    AB-AA-002
5 AB-AA-001    00-00-001
6 AB-AA-002    00-00-001
7 AA-AS-002    AB-AB-001
8 AB-AS-001    00-00-001想把s_ab字段里所有的00-00-001的s_aa字段为s_ab字段的数据修改为00-00-001,修改s_ab字段的数据时,s_aa字段里前2位要为'AA'.得到的结果如下:s_id   s_aa      s_ab
1 AA-AA-001   00-00-001
2 AA-AB-001   00-00-001
3 AA-AC-001   00-00-001
4 AA-AD-001   00-00-001
7 AA-AS-002   AB-AB-001

解决方案 »

  1.   

    update test set s_ab='00-00-001'
    where left(s_aa,2)='AA' AND right(s_aa,3)='001'
      

  2.   

    update TEST
    set s_ab='00-00-01'
    where left(s_aa,2)='AA'
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2009-10-21 22:18:06
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([s_id] int,[s_aa] varchar(9),[s_ab] varchar(9))
    insert [tb]
    select 1,'AA-AA-001','AB-AA-001' union all
    select 2,'AA-AB-001','AB-AA-002' union all
    select 3,'AA-AC-001','AB-AS-001' union all
    select 4,'AA-AD-001','AB-AA-002' union all
    select 5,'AB-AA-001','00-00-001' union all
    select 6,'AB-AA-002','00-00-001' union all
    select 7,'AA-AS-002','AB-AB-001' union all
    select 8,'AB-AS-001','00-00-001'
    --------------开始查询--------------------------
    update 
      tb 
    set 
      s_ab='00-00-001'
    where 
      left(s_aa,2)='aa' and right(s_aa,3)='001'select * from tb
    ----------------结果----------------------------
    /* s_id        s_aa      s_ab
    ----------- --------- ---------
    1           AA-AA-001 00-00-001
    2           AA-AB-001 00-00-001
    3           AA-AC-001 00-00-001
    4           AA-AD-001 00-00-001
    5           AB-AA-001 00-00-001
    6           AB-AA-002 00-00-001
    7           AA-AS-002 AB-AB-001
    8           AB-AS-001 00-00-001(8 行受影响)*/
      

  4.   


    if object_id('test') is not null
    drop table test
    go
    create table Test(s_id int,s_aa nvarchar(20),s_ab nvarchar(20))
    goinsert into Test select 
    1,'AA-AA-001','AB-AA-001' union all select
    2,'AA-AB-001','AB-AA-002' union all select
    3,'AA-AC-001','AB-AS-001' union all select
    4,'AA-AD-001','AB-AA-002' union all select
    5,'AB-AA-001','00-00-001' union all select
    6,'AB-AA-002','00-00-001' union all select
    7,'AA-AS-002','AB-AB-001' union all select
    8,'AB-AS-001','00-00-001' 
    goupdate Test Set s_ab='00-00-001' where left(s_aa,2)='AA' and right(s_aa,3)='001'
    select * from test where s_aa like 'AA%'
    /*s_id        s_aa                 s_ab
    ----------- -------------------- --------------------
    1           AA-AA-001            00-00-001
    2           AA-AB-001            00-00-001
    3           AA-AC-001            00-00-001
    4           AA-AD-001            00-00-001
    7           AA-AS-002            AB-AB-001(5 行受影响)
    */
      

  5.   

    ---------------------------------
    --  Author: HEROWANG(让你望见影子的墙)
    --  Date  : 2009-10-21 22:29:03
    ---------------------------------
     
    IF OBJECT_ID('[tb]') IS NOT NULL 
        DROP TABLE [tb]
    go
    CREATE TABLE [tb] (s_id INT,s_aa VARCHAR(9),s_ab VARCHAR(9))
    INSERT INTO [tb]
    SELECT 1,'AA-AA-001','AB-AA-001' UNION ALL
    SELECT 2,'AA-AB-001','AB-AA-002' UNION ALL
    SELECT 3,'AA-AC-001','AB-AS-001' UNION ALL
    SELECT 4,'AA-AD-001','AB-AA-002' UNION ALL
    SELECT 5,'AB-AA-001','00-00-001' UNION ALL
    SELECT 6,'AB-AA-002','00-00-001' UNION ALL
    SELECT 7,'AA-AS-002','AB-AB-001' UNION ALL
    SELECT 8,'AB-AS-001','00-00-001'select * from [tb]update s
    set s.s_ab='00-00-001'
    from tb s join tb t on s.s_ab=t.s_aa and t.s_ab='00-00-001'
    where left(s.s_aa,2)='AA'1 AA-AA-001 00-00-001
    2 AA-AB-001 00-00-001
    3 AA-AC-001 00-00-001
    4 AA-AD-001 00-00-001
    5 AB-AA-001 00-00-001
    6 AB-AA-002 00-00-001
    7 AA-AS-002 AB-AB-001
    8 AB-AS-001 00-00-001