要求,数据库表中基于字段A,B的值生成字段C的值。具体如下:数据表插入数据时,字段A与B的值手工插入,字段C的值根据字段A与B的值自动生成,规则如下:if (A == '1' && B == '1')
{ C = '1'; }
if (A == '1' && B == '2')
{ C = '2'; }
if (A == '2' && B == '1')
{ C = '3'; }
if (A == '2' && B == '2')
{ C = '4'; }
else
{ C = '0'; }如何实现?
是不是要写个触发器或者存储过程,,请指点。如果要写给个代码吧, 谢谢了。。

解决方案 »

  1.   

    create table test(a int,b int,c as case when a=1 and b=1 then 1 when a=1 and b=2 then 2 when a=2 and b=1 then 3 when a=2 and b=2 then 4 else 0 end)
      

  2.   

    貌似C字段没什么用,不用查询时用case whenselect a , b , c = (case when a=1 and b=1 then 1 when a=1 and b=2 then 2 when a=2 and b=1 then 3 when a=2 and b=2 then 4 else 0 end) from tb
      

  3.   

    if (A == '1' && B == '1')
    { C = '1'; }
    if (A == '1' && B == '2')
    { C = '2'; }
    if (A == '2' && B == '1')
    { C = '3'; }
    if (A == '2' && B == '2')
    { C = '4'; }
    else
    { C = '0'; }
    只是个例子,如果很长怎么办??也要一句一句的写上去吗?
      

  4.   

    1楼都帮你写好了,插入的时候insert test select 1,2