我通过create table 命令建多个表:
例如:
表1结构:
[ID] numeric(20) PRIMARY KEY
[PID] numeric(20) {允许重复值}
[Caption] varchar(30) {允许重复值}表2结构:
[sp_id] bigint Identity
[sp_lb] varchar(30) REFERENCES 表1(Caption) ON UPDATE CASCADE {允许重复值}
[NodeKey] numeric(20) REFERENCES 表1(PID) ON UPDATE CASCADE  {允许重复值}当表1的PID或Caption值发生改变时,表2内的sp_lb、NodeKey也发生相应的修改我想通过以上的方法实现但不行。不知道如何通过create table 实现它。
望高手帮忙。

解决方案 »

  1.   

    我这里没有安装mssql我大概写一下,create trigger  xxxx on 表一
    for update
    declare @id integer,
            @pid integer,
            @caption char(30)
    select @id=id,@pid=pid,@caption=caption from inserted
    update 表二 set nodekey=@pid,sp_lb=@caption where sp_id=@id
      

  2.   

    只用给PID和Caption字段的OnChange事件加上适当的代码就行了。
      

  3.   

    如果是单机版数据库同意  wizardqi(男巫) ( ) 信誉:100 的
    如果是网络数据库最好用触发器统一管理,否则你不能作为一个事物执行的话,操作是不安全的
      

  4.   

    create trigger tr_aaa on 表1
    for update
    as
      declare @a varchar(30) 
      declare @b varchar(30) 
      select @a=caption from deleted 
      select @b=caption from inserted   if (select count(*) from 表2 where caption=@a)
      begin
        updata 表2 set caption=@b where caption=@a 
      end;
      

  5.   

    to itlcx(飛龍):由于真的不会使用触发器,以下代码不太理解,能注释一下吗?
    以下代码是放在create table 中写呢,还是单独写到SQL中呢?
    create trigger tr_aaa on 表1
    for update
    as
      declare @a varchar(30) 
      declare @b varchar(30) 
      select @a=caption from deleted 
      select @b=caption from inserted   if (select count(*) from 表2 where caption=@a)
      begin
        updata 表2 set caption=@b where caption=@a 
      end;
      

  6.   

    以上代码是写在建立表中
    declare @a varchar(30) 
      declare @b varchar(30) 
      select @a=caption from deleted 
      select @b=caption from inserted 
    以上意思为
    定义变量及赋值
      

  7.   

    在SQL Server2000中找到 表1
    然后右击,选 所有任务/管理触发器
    就是这里