本人刚开始学习transact—sql  遇到了问题   
create trigger overdraft  on account for update 
as 
if  inserted.balance < 0 
begin   
     insert into borrower 
     select customer_name,depositor.account_number  
     from depositor, inserted   
     where inserted.account_number =depositor.account_number
     insert into loan values
       (inserted.account_number, inserted.branch_name, -inserted.balance)
     update account  set balance = 0
     from account,  inserted 
     where account.account_number = inserted.account_number 
end
运行结果错误为:
消息 4104,级别 16,状态 1,过程 overdraft,第 3 行
无法绑定由多个部分组成的标识符 "inserted.balance"。
消息 4104,级别 16,状态 1,过程 overdraft,第 10 行
无法绑定由多个部分组成的标识符 "inserted.account_number"。
消息 4104,级别 16,状态 1,过程 overdraft,第 10 行
无法绑定由多个部分组成的标识符 "inserted.branch_name"。
消息 4104,级别 16,状态 1,过程 overdraft,第 10 行
无法绑定由多个部分组成的标识符 "inserted.balance"。

解决方案 »

  1.   

    --触发器的操作1create table 化验室纱组(本厂编号 int,客户 int,色号 int,纱支 int)
    create table 化验室布组(本厂编号 int,客户 int,色号 int,布类 int)
    go
    create trigger my_trig on 化验室纱组 for insert ,update ,delete
    as
    if not exists(select 1 from inserted)
       delete 化验室布组 from deleted t where 化验室布组.本厂编号 = t.本厂编号 
    else if not exists(select 1 from deleted) 
       insert into 化验室布组(本厂编号 ,客户 ,色号) select 本厂编号 ,客户 ,色号 from inserted
    else
       update 化验室布组 set 客户 = t.客户 , 色号 = t.色号 from inserted t where 化验室布组.本厂编号 = t.本厂编号
    go--1、insert 对化验室纱组插入数据,然后查看化验室布组表的数据
    insert into 化验室纱组 values(1 , 2 , 3 , 4)
    insert into 化验室纱组 values(5 , 6 , 7 , 8)
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    1           2           3           NULL
    5           6           7           NULL(所影响的行数为 2 行)
    */--2、update , 更改化验室纱组表中本厂编号=1的色号=6
    update 化验室纱组 set 色号 = 6 where 本厂编号 = 1
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    1           2           6           NULL
    5           6           7           NULL(所影响的行数为 2 行)
    */--3、delete 化验室纱组表中本厂编号=1的那条数据
    delete from 化验室纱组 where 本厂编号 = 1
    go
    select * from 化验室布组
    /*
    本厂编号        客户          色号          布类          
    ----------- ----------- ----------- ----------- 
    5           6           7           NULL(所影响的行数为 1 行)
    */drop table 化验室纱组 , 化验室布组
      

  2.   

     insert into loan values
      (inserted.account_number, inserted.branch_name, -inserted.balance)
    这句改掉insert  into loan
    select account_number,branch_name,balance
    from inserted
      

  3.   

    按照你说的改了之后还是这个问题 
    消息 4104,级别 16,状态 1,过程 overdraft,第 3 行
    无法绑定由多个部分组成的标识符 "inserted.balance"。是不是这句话有问题啊
    if inserted.balance < 0 
      

  4.   

    你错在:
    不能直接 if inserted.balance<0
    因为 balance 不是一个变量,而是表中的一个列值,无法直接获得此列值,而且此列值可能并非一个值.
    更改:
    create trigger overdraft on account for update  
    as  
    if exists(select 1 from inserted where balance < 0 )
    begin   
      insert into borrower  
      select customer_name,depositor.account_number   
      from depositor, inserted   
      where inserted.account_number =depositor.account_number
      insert into loan values
      (inserted.account_number, inserted.branch_name, -inserted.balance)
      update account set balance = 0
      from account, inserted  
      where account.account_number = inserted.account_number  
    end
    此处,不知道你的触发器中的语句干什么,只是改了if 后的条件语句,供参考.
      

  5.   


    逻辑表  Inserted 不能用 inserted.balance 形式引用字段。  --inserted.balance < 0 地方使用错误。
    修改为 :if exists(select 1 from inserted where balance < 0 )
      

  6.   

    create trigger overdraft on account for update
    as  
    if exists(select * from inserted where balance <0 )
    begin   
         insert into borrower 
         select customer_name,depositor.account_number  
         from depositor, inserted   
         where inserted.account_number=depositor.account_number and inserted.balance<0
         insert into loan 
            select account_number,branch_name, -balance
            from inserted
            where balance<0
         update account  set balance = 0
         from account,inserted 
         where account.account_number = inserted.account_number  and inserted.balance<0
    end
    这个触发器的意思是当balance中出现负值时就将account的account——number作为loan——number,
    现在成功创建了trigger 可是执行这个句子update account set balance=-1992
    where account_number='A-217'时 
    出现问题
    (1 行受影响)(1 行受影响)(1 行受影响)(1 行受影响)
    消息 2627,级别 14,状态 1,过程 overdraft,第 9 行
    违反了 PRIMARY KEY 约束 'PK_loan'。不能在对象 'dbo.loan' 中插入重复键。
    语句已终止。我把loan中的主键去掉  就可以在loan中看到插入了2行重复数据~~为什么会插入两行?