我有两个表 
表 aname Amount 
李四  50
李四   60
张三   20
王麻   15
李四   60
张三   20
王麻   15表B
name    total
李四    ?
张三    ?
王麻   ?求,每次在A表添加一项目,就会更新B表对应的人名的总数!如,我在A表添加一条李四的数据,然后,就更新 B表的李四的 tatal 的值,这个值是来自A表的所有的李四的Amount 总和!

解决方案 »

  1.   

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO/****************************************************************************************/
    /* Make    2007/10/10    */
    /****************************************************************************************/
    ALTER PROCEDURE [TABLE_UPDATE] /*== 引数宣言 ===========================================================================*/
    @Name AS VARCHAR(100)
    @Amount AS INT
    /*======================================================================================*/AS
    /*== 変数宣言 ======================================================================*/
     @COUNT AS INT
    /*=====================================================================================*/
    --BEGIN TRANSACTION 

    BEGIN
    INSERT INTO a VALUES(@Name,@Amount)

    BEGIN
    DECLARE TMP CURSOR FAST_FORWARD FOR 
    SELECT 
    NAME,
    COUNT(*) AS TOTAL
    FROM 
    A
    GROUP BY NAME 
    END

    BEGIN
    TRUNCATE TABLE b
     
    OPEN TMP 
    FETCH NEXT FROM TMP INTO 
    @NAME, 
    @TOTAL 
    WHILE @@FETCH_STATUS=0 
    BEGIN 
    INSERT INTO TABLE a VALUES(@NAME, @TOTAL) 
    END 
    CLOSE TMP 
    DEALLOCATE TMP 
    END
    END
      

  2.   

    declare  @name nvarchar(50)  
    declare  @value int 
    set @name='张三' --姓名为张三
    set @value=60   --分数是60分insert into 表 a([name],Amount) values (@name,@value)  --在表A插入一条记录
    declare  @count int    --检查表B是否包含姓名为张三的记录
    declare  @sum int  --表A的Amount的总和
    select @sum=sum(Amount) from 表 a      ----表A中的姓名为张三的Amount的总和
    select @count=count(*) from 表 b where [name]=@name  --统计表B中的姓名为张三的记录数
    if @count=0  --如果表B不存在着姓名为张三的话,就增加记录
    begin 
    insert into 表b([name],total)  values (@name,@sum)
    end 
    else       --存在姓名为张三就修改记录total的值 
    begin 
    update  表b set total=@sum where name=@name
    end 
      

  3.   

    在服务器端执行上述代码,将上面代码中的
    ALTER PROCEDURE [TABLE_UPDATE]
    更改为:
    CREATE PROCEDURE [TABLE_UPDATE]在程序中调用,用sqlcommand.addvalue()方法,将参数传入。
    用stored produce运行以上[TABLE_UPDATE]
      

  4.   

    建议使用存储过程
    在每次对a表添加数据后,更新b表,假设:传入数据a表数据为 @Name = '李四', @Amount = 80
    insert into a( name , Amount )
    value( @Name , @Amount  )declare @tempValue int
    if exists( select * from b where name = @Name )
    begin
      select @tempValue = total from b where name = @Name 
      update b set total = sum(@tempValue+@Amount) where name = @Name 
    end
    else
    begin
      insert into b( name, total )
      value( @Name, @Amount )
    end
      

  5.   

    这个一句 运行可以declare  @name nvarchar(50)   
    declare  @Amount int  
    set @name= '张三' 
    set @Amount=60   insert into a( name , Amount ) values( @Name , @Amount  ) 
    declare @tempValue int 
    if exists( select * from b where name = @Name ) 
    begin 
      select @tempValue = total from b where name = @Name  
      update b set total = @tempValue+@Amount where name = @Name  
    end 
    else 
    begin 
      insert into b( name, total ) values( @Name, @Amount ) 
    end 
      

  6.   

    @name,@Amount在cs代码里传参数句行了,string.format("...set @name='{0}' set @Amount={1}...",strName,Amount);