create table table_3
(
table3_num int not null,
table3_count  as (table4_num)
)
go我的意思是,怎么设置 table3_count 的默认值为 table3_num 的值? 我要做到以后能修改的,貌似用计算列实之后,就不能改了?忘指点!! 谢谢!

解决方案 »

  1.   

    用SQL没办法实现啊? 那看样子只能在业务逻辑里面实现了。。
      

  2.   

    create table table_3
    (
    table3_num int not null,
    table3_count int
    )
    gocreate trigger tr_table_3
    on table_3 instead of insert
    as
    set nocount on
    insert table_3 select table3_num, isnull(table3_count, table3_num) from inserted
    set nocount off
    goinsert table_3(table3_num) select 1
    insert table_3 select 2,10select * from table_3
    /*
    table3_num  table3_count
    ----------- ------------
    1           1
    2           10
    */
      

  3.   

    其实这个问题,你允许table3_count空好了,显示的时候:select table3_num,  isnull(table3_count, table3_num) from table_3一样达到效果,不一定非要把table3_num的列值保存到table3_count。
      

  4.   

    DROP TABLE Table_3
    create table table_3
    (
    table3_num int ,
    table3_count INT
    )
    GO
    ---DROP TRIGGER trig_tab3
    CREATE TRIGGER trig_tab3
    ON table_3 INSTEAD  OF INSERT 
    AS 
    INSERT INTO table_3 SELECT table3_num,table3_num FROM inserted
    GOINSERT INTO table_3 (table3_num) SELECT 5SELECT * FROM table_3 ttable3_num  table3_count
    ----------- ------------
    1           1
    5           5(2 row(s) affected)
      

  5.   

    完全可以将table3_count 留空,然后如果前台程序调用,可以采用table3_num的值替代,参考isnull函数。
      

  6.   

    个人认为 
    建表就不用要 table3_count 列建个视图使用时直接调用视图 
    没必要对一条记录 进行两次 insert 或 一次insert 一次update
    个人意见 
      

  7.   

    我怎么理解不了你的意思?
    这样?
    create table table_3
    (
    table3_num int not null,
    table3_count as table3_num
    )
    go
      

  8.   

    --其实你只需加个约束 ,默认值就行了。
    create table table_3
    (
    table3_num int not null,
    )
    go
    alter table table_3 add check ALTER TABLE table_3  --增加一列
    ADD table3_count int NULL
    CONSTRAINT table3_countConstraint
    DEFAULT  table3_num WITH VALUES