Sql 插入触发器更新数据在Sql中,写一个触发器,
        表结构:表名:Users
                 id(主键)    name    sign  state 
                 1           小明    备注     1
                 2           小张    备注     1
                 3           小刘    备注     1
                 4           小罗    备注     1
                 5           张三    备注     1
实现结果:插入相同id的数据,删除原来的相同数据,插入新的数据 把state状态改为1
     sql:INSERT INTO Users ([id],[name],[state],[sign]) values ( 1,'李四','备注',0)
     删除 id 等于 1 的数据,输入新数据    结果:
                id(主键)     name    sign  state 
                 1           李思    备注     1
                 2           小张    备注     1
                 3           小刘    备注     1
                 4           小罗    备注     1
                 5           张三    备注     1求这个触发器怎么写

解决方案 »

  1.   

    create trigger tir_name on tb
    for insert 
    as
     delete tb from inserted where tb.id=inserted.id
     insert tb select inserted
      

  2.   

    id(主键)
    是主键,不能再插入id相同的值,用触发器做不到,得用判断语句.
      

  3.   

    create trigger tir_users on Users
    instead of insert 
    as
     delete tb from inserted where tb.id=inserted.id
     insert tb select inserted
      

  4.   

    --更改
    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([id] int,[name] nvarchar(2),[sign] nvarchar(2),[state] int)
    Insert tb
    select 1,N'小明',N'备注',1 union all
    select 2,N'小张',N'备注',1 union all
    select 3,N'小刘',N'备注',1 union all
    select 4,N'小罗',N'备注',1 union all
    select 5,N'张三',N'备注',1
    Go
    if object_id('tir_name')is not null drop trigger tir_name
    go
    create trigger tir_name on tb
    for insert 
    as
     delete tb from tb,inserted where tb.id=inserted.id
     insert tb select * from inserted
    go
    INSERT INTO tb([id],[name],[sign],[state]) values ( 1,N'李四',N'备注',0)
    select * from tb
    /*
    id          name sign state
    ----------- ---- ---- -----------
    1           李四   备注   0
    2           小张   备注   1
    3           小刘   备注   1
    4           小罗   备注   1
    5           张三   备注   1(5 個資料列受到影響)
    */
      

  5.   

    在上面的触发器中再加一个update
     delete tb from tb,inserted where tb.id=inserted.id
     insert tb select * from inserted
     update tb set state  = 1 from tb where id = (select id from inserted)
      

  6.   


    4楼的结果是因为他的id没有设为主键
    设为主键后执行,错误为/*
    服务器: 消息 2627,级别 14,状态 1,行 1
    违反了 PRIMARY KEY 约束 'PK_tb'。不能在对象 'tb' 中插入重复键。
    语句已终止。(所影响的行数为 5 行)
    */
      

  7.   

    Create table Users([id] int primary key,[name] nvarchar(2),[sign] nvarchar(2),[state] int)
    Insert Users
    select 1,N'小明',N'备注',1 union all
    select 2,N'小张',N'备注',1 union all
    select 3,N'小刘',N'备注',1 union all
    select 4,N'小罗',N'备注',1 union all
    select 5,N'张三',N'备注',1
    Gocreate trigger tir_users on Users
    instead of insert 
    as
    begin
     delete Users from inserted where Users.id=inserted.id
     insert Users select [id],[name],[sign],1 from inserted
    end
    goINSERT INTO Users([id],[name],[sign],[state]) values ( 1,N'李四',N'备注',0)
    select * from usersdrop table Users
    id          name sign state       
    ----------- ---- ---- ----------- 
    1           李四   备注   1
    2           小张   备注   1
    3           小刘   备注   1
    4           小罗   备注   1
    5           张三   备注   1(所影响的行数为 5 行)
      

  8.   


    id没有设为主键
    设为主键后执行,错误为
    /*
    服务器: 消息 2627,级别 14,状态 1,行 1
    违反了 PRIMARY KEY 约束 'PK_tb'。不能在对象 'tb' 中插入重复键。
    语句已终止。(所影响的行数为 5 行)
    */