发布网友 发布时间:2022-04-08 18:38
共3个回答
懂视网 时间:2022-04-08 22:59
***** 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触发器,限制一次只能删除一条数据
标签:
热心网友 时间:2022-04-08 20:07
可以,触发器功能包括插入和删除以及更新,更新就是先删除后插入的合体.你所说的可以做到 但你说的条件不明确,触发器只是一个定义或者说是先决条件,并不能直接插入的,需要你用语句去激活它才会执行它的任务热心网友 时间:2022-04-08 21:25
不是,触发器仅仅是一个执行sql语句的条件,当条件满足(譬如插入、删除、更新记录)时,可以出发你定义的一条或多条sql语句。例如,当insert一条记录时,同时更新table1的记录和删除table2的记录,可能就是如下: