我的数据库中有自己的触发器。但我的客户想要定制。
我需要根据他想要的优先顺序给出触发顺序。
我知道我可以用 1 个触发器完成所有这些操作,如果是背靠背的话。
但我想 custom_xCompanyTrigger 我的客户想要的触发器,并按照我想要的顺序放置。
如果有多个触发器,它要么按创建顺序工作,要么按名称顺序工作,我无法理解。
这样,我应该如何进行以客户为基础的定制?
CREATE TABLE [dbo].[MyTable](
[name] [varchar](50) NULL,
[city] [varchar](50) NULL,
[date] [date] NULL,
[value] [int] NULL,
[islocked] [int] NULL
) ON [PRIMARY]
INSERT [dbo].[MyTable] ([name], [city], [date], [value], [islocked]) VALUES (N'John', N'Berlin', CAST(N'2022-01-02' AS Date), 10,1)
INSERT [dbo].[MyTable] ([name], [city], [date], [value], [islocked]) VALUES (N'Marty', N'Istanbul', CAST(N'2022-01-03' AS Date), 20, 1)
INSERT [dbo].[MyTable] ([name], [city], [date], [value], [islocked]) VALUES (N'Aila', N'Washington', CAST(N'2022-01-05' AS Date), 30, 1)
INSERT [dbo].[MyTable] ([name], [city], [date], [value], [islocked]) VALUES (N'Aise', N'Moskova', CAST(N'2022-01-01' AS Date), 40, 1)
4 rows affected
create TRIGGER MyTable_Trigger_Delete2 ON dbo.MyTable
AFTER DELETE AS
BEGIN
/* Control-2 */
IF ((SELECT COUNT(*) FROM DELETED WHERE date<='2022-01-04')>0)
BEGIN
PRINT 'Control-2 worked.'
RAISERROR ('You cannot delete records before January 5th.', 16, 1)
ROLLBACK TRANSACTION
END
else
PRINT 'Control-2 passed.'
END
create TRIGGER MyTable_Trigger_Delete1 ON dbo.MyTable
AFTER DELETE AS
BEGIN
/* Control-1 */
IF ((SELECT COUNT(*) FROM DELETED WHERE islocked=1)>0)
BEGIN
PRINT 'Control-1 worked.'
RAISERROR ('You cannot delete a locked recording.', 16, 1)
ROLLBACK TRANSACTION
END
else
PRINT 'Control-1 passed.'
END
select * from MyTable
姓名 | 城市 | 日期 | 价值 | 被锁住了 |
---|---|---|---|---|
约翰 | 柏林 | 2022-01-02 | 10 | 1个 |
马蒂 | 伊斯坦布尔 | 2022-01-03 | 20 | 1个 |
艾拉 | 华盛顿 | 2022-01-05 | 30 | 1个 |
爱色 | 莫斯科娃 | 2022-01-01 | 40 | 1个 |
delete from MyTable where city='Berlin'
Msg 50000 Level 16 State 1 Line 11
You cannot delete records before January 5th.
Msg 3609 Level 16 State 1 Line 1
The transaction ended in the trigger. The batch has been aborted.
select * from MyTable
姓名 | 城市 | 日期 | 价值 | 被锁住了 |
---|---|---|---|---|
约翰 | 柏林 | 2022-01-02 | 10 | 1个 |
马蒂 | 伊斯坦布尔 | 2022-01-03 | 20 | 1个 |
艾拉 | 华盛顿 | 2022-01-05 | 30 | 1个 |
爱色 | 莫斯科娃 | 2022-01-01 | 40 | 1个 |