标题:请教触发该怎么写
只看楼主
jack_xu2046
Rank: 2
等 级:论坛游民
帖 子:72
专家分:11
注 册:2008-10-1
结帖率:81.82%
已结贴  问题点数:10 回复次数:2 
请教触发该怎么写
不知怎么用
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,Name>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================
CREATE TRIGGER <Schema_Name, sysname, Schema_Name>.<Trigger_Name, sysname, Trigger_Name>
   ON  <Schema_Name, sysname, Schema_Name>.<Table_Name, sysname, Table_Name>
   AFTER <Data_Modification_Statements, , INSERT,DELETE,UPDATE>
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here

END
GO

以上是数据库中新建触发器中默认的。



假如SEQUENCE=4 这一行删除 如何添加删除触发使得 下面 5,6,7等自动减去1
还有如何放一个update触发,使得将4手动改成5后 5,6,7等自动加上1.
搜索更多相关主题的帖子: Create 
2011-01-17 12:42
jack_xu2046
Rank: 2
等 级:论坛游民
帖 子:72
专家分:11
注 册:2008-10-1
得分:0 
ALTER TRIGGER [dbo].[AutoUpdateSequence]
   ON  [dbo].[ROUTING]
   AFTER UPDATE
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
DECLARE @mROUTE NVARCHAR(40)
DECLARE @mSEQUENCE SMALLINT
DECLARE @mSTATION nvarchar(20)
DECLARE @mint smallint
set @mint = 1
select @mROUTE = ROUTE,@mSEQUENCE =SEQUENCE,@mSTATION =STATION FROM INSERTED
UPDATE ROUTING SET SEQUENCE = SEQUENCE + @mint WHERE ROUTE =@mROUTE AND SEQUENCE > @mSEQUENCE
END
参照论坛中一些例子,写了一个update的触发。运行结果有点不好,
假如 sequence =7这一行改成sequence =8 想要的结果是下面两个sequence 应该自动变成 9,10
可是是之前的sequence =8没有变,而且按一下执行按钮后sequence =9的才变成sequence =10


菜鸟中的菜鸟
2011-01-17 14:09
C心情
Rank: 2
等 级:论坛游民
帖 子:90
专家分:74
注 册:2008-1-19
得分:10 
最好自己写个存储过程,个人觉得少用触发器的好!
2011-01-24 17:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-331194-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.163233 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved