表1
ID  字段
A1  ...
A2  ..
B1
B2
...
现在我想加一个字段,'地区',用来表是ID所属的地区.他和ID 的关系在表2里
ID 地区
A1  A
A2  A
B1  B
B2  B
..我现在的程序不断往表1里加数据,要求'地区'列的内容也时时加进去.怎么实现呢?

解决方案 »

  1.   

    1.先替换表1已有的数据.
    update tb1 set 地区 = tb2.地区 from tb1 , tb2 where tb1.id = tb2.id
    2.对表1创建触发器.
    CREATE TRIGGER my_trig ON tb1 FOR INSERT
    as
      update tb1 set 地区 = (select 地区 from tb2 , inserted where tb2.id = inserted.id) where id = (select id from inserted)
    go
      

  2.   

    同意,不加也罢,最后用视图即可.create view my_view
    as
      select tb1.id , tb2.地区 from tb1 , tb2 where tb1.id = tb2.id
    go
      

  3.   


    --> (让你望见影子的墙)生成测试数据,时间:2008-12-22
     
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([ID] nvarchar(2),地区 nvarchar(1))
    Insert tb
    select N'A1',null union all
    select N'A2',null union all
    select N'B1',null union all
    select N'B2',null
    Go
    Select * from tbupdate tb
    set 地区=left(id,1)
    select * from tbA1 A
    A2 A
    B1 B
    B2 B