我想实现如下功能:在同一个表中有三列a、b、c,输入一条记录:在输入a列、b列的数值后(未保存此记录时),自动填入C列的数值--等于a列乘以b列,触发器可以实现吗?

解决方案 »

  1.   

    For example:create table test(a int,b int,c as a*b)
    goinsert into test(a,b) values(5,6)
    select * from test
    go/*
    a           b           c           
    ----------- ----------- ----------- 
    5           6           30
    */drop table test
    go
      

  2.   

    问题是现在应用的软件中的表结构都已经建好了,就是说a\b\b列本来都是需要输入的,我现在想通过触发器实现输入a、b列自动计算出c列
      

  3.   

    钻石已经说了不用触发器了直接加一列c
    然后在表达式上写 a*bc根本就是多余的
    要用的时候可直接 select a,b,a*b as c from tab
      

  4.   

    没明白我的意思:
    现在使用的是一套商品化软件,表结构都是定死的,原来a\b\c列之间没有任何关系,我想设定这样一种关系:c=a*b,输入a\b后自动计算c,而且是在该记录保存之前计算。
      

  5.   

    create trigger tri_insert on t
    after update,insert 
    as
    update t 
    set c=t1.a*t1.b from inserted t1
    where t.id=t1.id
      

  6.   

    create table t
    (
    id int identity(1,1),
    a int,
    b int,
    c int
    )create trigger aa on t
    after insert ,update
    as
    update t
    set c=t1.a*t1.b
    from inserted t1
    where t.id=t1.idinsert into t
    select 12,10 ,null union all
    select 5,6,nullselect * from tid          a           b           c           
    ----------- ----------- ----------- ----------- 
    1           12          10          120
    2           5           6           30(2 row(s) affected)