我现在有2张表,分别为表1 表2
表1结构为:
记录号     物品编号     数目
1          1             2
2          1             3
3          1             4
4          2             1
5          2             10
6          3             5
7          3             7

表2结构为:
物品编号           总数目
1                    9
2                    11
3                    12
。。
现在我想在表1中用加个触发器
当表1中有删除 插入 修改操作时候
表2中的物品编号的总数目也相应发生变化,请问各位高手怎么实现,谢谢啦!!   

解决方案 »

  1.   

    --------------------SQL Server数据格式化工具-------------------
    ---------------------------------------------------------------
    -- DESIGNER :happycell188(喜喜)
    --       QQ :584738179
    -- Development Tool :Microsoft Visual C++ 6.0    C Language 
    -- FUNCTION :CONVERT DATA TO T-SQL
    ---------------------------------------------------------------
    -- Microsoft SQL Server  2005
    -- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
    ---------------------------------------------------------------
    ---------------------------------------------------------------use test
    go
    if object_id('test.dbo.tb1') is not null drop table tb1
    -- 创建数据表
    create table tb1
    (
    记录号 int identity,
    物品编号 int,
    数目 int
    )
    go
    --插入测试数据
    insert into tb1 select 1,2
    union all select 1,3
    union all select 1,4
    union all select 2,1
    union all select 2,10
    union all select 3,5
    union all select 3,7
    go
    if object_id('test.dbo.tb2') is not null drop table tb2
    -- 创建数据表
    create table tb2
    (
    物品编号 int,
    总数目 int
    )
    go
    --插入测试数据
    insert into tb2 select 1,9
    union all select 2,11
    union all select 3,12
    go
    --代码实现create trigger tri_test
    on tb1
    for insert,update,delete
    as
    begin
    declare @goodno1 int,@no1 int, @goodno2 int,@no2 int
    select @goodno1=物品编号,@no1=数目 from inserted
    select @goodno2=物品编号,@no2=数目 from deleted
    if @goodno1>0 and (@goodno2 is null)--插入操作处理
    update tb2 set 总数目=总数目+@no1 where 物品编号=@goodno1
    else if @goodno2>0 and (@goodno1 is null) --删除操作处理
    update tb2 set 总数目=总数目-@no2 where 物品编号=@goodno2
    else if @goodno1>0 and @goodno2>0 --更新操作处理
    update tb2 set 总数目=总数目+@no1-@no2  where 物品编号=@goodno1
    end
    go
    --------------------------------
    --原数据记录
    select * from tb1
    select * from tb2
    /*
    tb1
    记录号 物品编号 数目
    -----------------------
    1 1 2
    2 1 3
    3 1 4
    4 2 1
    5 2 10
    6 3 5
    7 3 7tb2
    物品编号 总数目
    -----------------------
    1 9
    2 11
    3 12
    */
    -----------------------------------------
    --插入数据记录实例
    insert into tb1 select 2,4
    go
    select * from tb1 
    select * from tb2
    /*结果
    tb1
    记录号 物品编号 数目
    -----------------------
    1 1 2
    2 1 3
    3 1 4
    4 2 1
    5 2 10
    6 3 5
    7 3 7
    8 2 4   --插入的新记录tb2
    物品编号 总数目
    -----------------------
    1 9
    2 15   --增加4
    3 12
    */
    -----------------------------------------
    --删除数据记录实例
    delete from tb1 where 物品编号=2 and 数目=4
    go
    select * from tb1 
    select * from tb2
    /*结果
    tb1
    记录号 物品编号 数目
    -----------------------
    1 1 2
    2 1 3
    3 1 4
    4 2 1
    5 2 10
    6 3 5
    7 3 7tb2
    物品编号 总数目
    -----------------------
    1 9
    2 11
    3 12
    */
    -----------------------------------------
    --更新数据记录实例
    update tb1 set 数目=5 where 物品编号=2 and 数目=10
    go
    select * from tb1 
    select * from tb2
    /*结果
    tb1
    记录号 物品编号 数目
    -----------------------
    1 1 2
    2 1 3
    3 1 4
    4 2 1
    5 2 5   --数目由10变为5
    6 3 5
    7 3 7tb2
    物品编号 总数目
    -----------------------
    1 9
    2 6   --总数目减5
    3 12
    */