码迷,mamicode.com
首页 > 数据库 > 详细

T-SQL触发器,限制一次只能删除一条数据

时间:2016-01-01 14:52:36      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

/****** Object:  Trigger [dbo].[trg_del]    Script Date: 01/01/2016 12:58:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[trg_del] ON [dbo].[Common_Header]
    INSTEAD OF DELETE
AS
    BEGIN
        DECLARE @cou INT
        DECLARE @headerid UNIQUEIDENTIFIER
        SELECT  @cou = COUNT(*)
        FROM    deleted;
        SELECT  @headerid = id
        FROM    deleted
        IF ( @cou > 1 )
            RAISERROR(数据不允许删除!, 16, 1)
        ELSE
            DELETE  FROM [Common_Header]
            WHERE   id = @headerid
    END
GO

 

CREATE TABLE [dbo].[Common_Header]
(
[ID] [uniqueidentifier] NOT NULL,
[Subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[ApplicantAD] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[ApplicantName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[CountryCode] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[CountryName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[CompanyCode] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[CompanyName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[DivisionCode] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[DivisionName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[LBUCode] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[LBUName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[CostCenter] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL,
[Email] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL,
[CreatedDate] [datetime] NULL,
[QueueID] [uniqueidentifier] NULL,
[Status] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Common_Header] ADD CONSTRAINT [PK_Common_Header] PRIMARY KEY CLUSTERED  ([ID]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20150603-113054] ON [dbo].[Common_Header] ([QueueID]) ON [PRIMARY]
GO

 

T-SQL触发器,限制一次只能删除一条数据

标签:

原文地址:http://www.cnblogs.com/wanghaibin/p/5093208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!