我有一个触发器可以防止有人使用Management Studio:
CREATE TRIGGER [TR_LOGON_APP]
ON ALL SERVER
FOR LOGON
AS
BEGIN
DECLARE @program_name NVARCHAR(128)
DECLARE @host_name NVARCHAR(128)
SELECT
@program_name = program_name,
@host_name = host_name
FROM
sys.dm_exec_sessions AS c
WHERE
c.session_id = @@spid
IF @program_name LIKE '%Management%Studio%'
BEGIN
RAISERROR('Access Denied.',16,1)
ROLLBACK;
END
END;
如何检查是否已存在,触发器将首先被丢弃?
我尝试过使用:
IF EXISTS (SELECT * FROM sys.objects WHERE [name] = N'TR_LOGON_APP' AND [type] = 'TR')
BEGIN
DROP TRIGGER [TR_LOGON_APP];
END;
和
IF EXISTS (SELECT * FROM sys.objects WHERE [name] = N'TR_LOGON_APP' AND [type] = 'TR')
BEGIN
DROP TRIGGER [TR_LOGON_APP] ON ALL SERVER;
END;
但两者都返回错误:
System.Data.OleDb.OleDbException(0x80040E14):已经存在一个名为' TR_LOGON_APP'的对象。在数据库中。
答案 0 :(得分:2)
sys.objects
目录视图包含数据库对象,而不包含服务器级对象。请改用sys.server_triggers
:
IF EXISTS(SELECT 1 FROM sys.server_triggers WHERE name = N'TR_LOGON_APP')
BEGIN
DROP TRIGGER TR_LOGON_APP ON ALL SERVER;
END;