可以用存储过程 `.`
create proc prc_1 @var int 
as
begin
update table1表 set 字段3=字段3*@var
end
...........................................

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/5429/5429535.xml?temp=.1490595
    这个内容和你差不多,你参考参考.
      

  2.   

    不是 ankor 说的,还有一个递归的意思在里面
      

  3.   

    写一个递归取树的叶节点的函数,比较简单,给你一个例子,只需增加数量累计就可满足你的需求:--生成测试数据
    create table BOM(ID INT,PID INT,MSG VARCHAR(1000))
    insert into BOM select 1,0,NULL
    insert into BOM select 2,1,NULL
    insert into BOM select 3,1,NULL
    insert into BOM select 4,2,NULL
    insert into BOM select 5,3,NULL
    insert into BOM select 6,5,NULL
    insert into BOM select 7,6,NULL
    go--创建用户定义函数
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),Level INT)
    as
    begin
        declare @i int,@ret varchar(8000)
        set @i = 1
        insert into @t select ID,PID,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        
        delete t from @t t where exists(select 1 from @t where PID=t.ID)
        
        return
    end
    go--执行查询
    select ID from dbo.f_getChild(1)
    go--输出结果
    /*
    4
    7
    */--删除测试数据
    drop function f_getChild
    drop table BOM
      

  4.   

    稍稍改造一下,就比较接近了:--生成测试数据
    create table BOM(ID INT,PID INT,NUM INT)
    insert into BOM select 1,0,1
    insert into BOM select 2,1,2
    insert into BOM select 3,1,3
    insert into BOM select 4,1,2
    insert into BOM select 5,2,2
    insert into BOM select 6,3,1
    insert into BOM select 7,6,2
    insert into BOM select 8,6,1
    insert into BOM select 9,6,3
    go--创建用户定义函数
    create function f_getChild(@ID VARCHAR(10))
    returns @t table(ID VARCHAR(10),PID VARCHAR(10),NUM INT,Level INT)
    as
    begin
        declare @i int,@ret varchar(8000)
        set @i = 1
        insert into @t select ID,PID,NUM,@i from BOM where PID = @ID
        
        while @@rowcount<>0
        begin
            set @i = @i + 1
            
            insert into @t 
            select 
                a.ID,a.PID,a.NUM*B.NUM,@i 
            from 
                BOM a,@t b 
            where 
                a.PID=b.ID and b.Level = @i-1
        end
        
        delete t from @t t where exists(select 1 from @t where PID=t.ID)
        
        return
    end
    go--执行查询
    select ID,NUM from dbo.f_getChild(1)
    go--输出结果
    /*
    ID         NUM         
    ---------- ----------- 
    4          2
    5          4
    7          6
    8          3
    9          9
    */--删除测试数据
    drop function f_getChild
    drop table BOM