我数据库中有一个单位(unit)表,有另外表使用了这个单位表的code列。
code          cndesc            endesc           valid
0001            个              piece              0
0002            箱                lots             0
0003            piece           piece              1
0004            lots            lots               1
这个是单位表中的一部分数据,现在的问题是,使用这个表的code字段的表有些code我要更新一下,
就是引用code=‘0001’的表中数据的code我要改成0001中endesc=cndesc的code。
就是引用表中code=‘0001’改成code=‘0003’
不要用update 表 set code =‘0003’ where code =‘0001’这样的语句给我。
因为我不知道引用表中会有哪些code使用了,哪些没使用,

解决方案 »

  1.   

    再补充下问题:
    就是以前的一些单位数据时不用了,用的是全英文的,我现在就是要把数据库以前的数据都更改成当前code对应的数据行的endesc相同的valid=1的数据行对应的code
      

  2.   

    UPDATE U SET CODE=C.CODE FROM  unit U ,TBC C WHERE 连接条件
      

  3.   

    要修改的表有cndesc endesc 这两个字段吗?
      

  4.   

    UPDATE a SET CODE=b.CODE FROM  unit a ,unit b WHERE a.endesc=b.endesc and b.valid=1
      

  5.   

    update a set 
        a.code=b.code
    from 要改的表 a
      join unit b
         on a.cndesc=b.endesc and b.cndesc<>b.endesc 要是有的话,TRY
      

  6.   

    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    create table [ta]([code] varchar(4),[cndesc] varchar(5),[endesc] varchar(5),[valid] int)
    insert [ta]
    select '0001','个','piece',0 union all
    select '0002','箱','lots',0 union all
    select '0003','piece','piece',1 union all
    select '0004','lots','lots',1
    --------------------------------
    update [ta] set [code]=(select min(code) from [ta] where endesc=cndesc)
    where code='0001'select *  from ta
    /*
    code cndesc endesc valid
    ---- ------ ------ -----------
    0003 个      piece  0
    0002 箱      lots   0
    0003 piece  piece  1
    0004 lots   lots   1(4 行受影响)*/
      

  7.   

    明白,改基础表的工作量胜过于改关联的表,因为你的所有关联表只是关联到基础表的CODE.
      

  8.   

    --> 生成测试数据表:unitIF NOT OBJECT_ID('[unit]') IS NULL
    DROP TABLE [unit]
    GO
    CREATE TABLE [unit]([code] NVARCHAR(10),[cndesc] NVARCHAR(10),[endesc] NVARCHAR(10),[valid] INT)
    INSERT [unit]
    SELECT '0001',N'个','piece',0 UNION ALL
    SELECT '0002',N'箱','lots',0 UNION ALL
    SELECT '0003','piece','piece',1 UNION ALL
    SELECT '0004','lots','lots',1
    GO
    --SELECT * FROM [unit]-->SQL查询如下:
    --更新原CODE的名称
    update unit set
    cndesc=(select cndesc from unit t where t.[endesc]=unit.[endesc] and [valid]=1)
    where valid=0select * from unit
    /*
    code       cndesc     endesc     valid
    ---------- ---------- ---------- -----------
    0001       piece      piece      0
    0002       lots       lots       0
    0003       piece      piece      1
    0004       lots       lots       1(4 行受影响)
    */
    --更新valid标识
    update unit set valid=1-validselect * from unit
    /*
    code       cndesc     endesc     valid
    ---------- ---------- ---------- -----------
    0001       piece      piece      1
    0002       lots       lots       1
    0003       piece      piece      0
    0004       lots       lots       0(4 行受影响)
    */
    删除无用的资料
    delete unit where valid=0select * from unit
    /*
    code       cndesc     endesc     valid
    ---------- ---------- ---------- -----------
    0001       piece      piece      1
    0002       lots       lots       1(2 行受影响)
    */
      

  9.   

    根据htl258,libin_ftsafe,SQL77的解题思路做好了,分享下,声明2个表变量,一个保存valid=0的,一个保存valid=1的,
    最后就用htl258说的用update就ok啦!!
    最后,再次感谢htl258,libin_ftsafe,SQL77这3位大大提供的思路!!
      

  10.   

    ----加外键约束alter table A add constraint wjys foreign key(code)
         references B(code)