如何在 SQL 服务器触发器中进行 IF 检查
How to do a IF check within a SQL Server trigger
我的 table 有以下 Microsoft SQL 服务器触发器:
-- Insert statements for trigger here
DECLARE @AssignmentUID uniqueidentifier;
DECLARE @ResourceUID uniqueidentifier;
DECLARE @ResourceName nvarchar(255);
DECLARE @ResourceClaimsAccount nvarchar(255);
DECLARE @ProjectUID uniqueidentifier;
DECLARE @ProjectName nvarchar(255);
DECLARE @ProjectUrl nvarchar(1000);
DECLARE @TaskUID uniqueidentifier;
DECLARE @TaskName nvarchar(255);
DECLARE @TaskUrl nvarchar(1000);
DECLARE @UserId nvarchar(255);
DECLARE @PWAUrl nvarchar(255);
DECLARE @sql varchar(8000)
SELECT @AssignmentUID = INSERTED.AssignmentUID
FROM INSERTED;
SELECT
@TaskUID = MSP_EpmAssignment_UserView.TaskUID,
@TaskName = MSP_EpmTask_UserView.TaskName,
@ProjectUID = MSP_EpmAssignment_UserView.ProjectUID,
@ProjectName = MSP_EpmProject_UserView.ProjectName,
@ProjectUrl = MSP_EpmProject_UserView.ProjectWorkspaceInternalHRef,
@ResourceUID = MSP_EpmAssignment_UserView.ResourceUID,
@ResourceClaimsAccount = MSP_EpmResource_UserView.UserClaimsAccount,
@ResourceName = MSP_EpmResource_UserView.ResourceName
FROM
MSP_EpmAssignment_UserView
LEFT OUTER JOIN
MSP_EpmResource_UserView ON MSP_EpmAssignment_UserView.ResourceUID = MSP_EpmResource_UserView.ResourceUID
LEFT OUTER JOIN
MSP_EpmProject_UserView ON MSP_EpmAssignment_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
LEFT OUTER JOIN
MSP_EpmTask_UserView ON MSP_EpmAssignment_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID
WHERE
(MSP_EpmAssignment_UserView.AssignmentUID = @AssignmentUID);
INSERT INTO tablename
(ItemTitle, ParentName, ParentUrl, Loginname, SourceId, CreatedAt)
VALUES (@TaskName, @ProjectName, @ProjectUrl, RIGHT(@ResourceClaimsAccount, CHARINDEX('\', REVERSE('\' + @ResourceClaimsAccount)) - 1), 1, GETDATE());
如何在执行 INSERT INTO 语句之前检查@AssignmentUID AND @TaskUID 是否为 NOT NULL?是否还有优化此触发器的潜力?
IF (@AssignmentUID IS NOT NULL AND @TaskUID IS NOT NULL)
BEGIN
...
END
您需要以 set-based 的方式编写触发器,以处理 Inserted
table 可能包含多行的事实。
试试这个代码 - 它使用你的核心 SELECT
并对你试图在开始。这将立即处理 Inserted
table 中包含的 所有 行 - 没有循环,没有游标 - 不需要这样混乱的代码!
INSERT INTO tablename (ItemTitle, ParentName, ParentUrl, Loginname, SourceId, CreatedAt)
SELECT
MSP_EpmTask_UserView.TaskName,
MSP_EpmProject_UserView.ProjectName,
MSP_EpmProject_UserView.ProjectWorkspaceInternalHRef,
RIGHT(MSP_EpmResource_UserView.UserClaimsAccount, CHARINDEX('\', REVERSE('\' + MSP_EpmResource_UserView.UserClaimsAccount)) - 1,
1,
SYSDATETIME()
FROM
MSP_EpmAssignment_UserView
INNER JOIN
Inserted i ON MSP_EpmAssignment_UserView.AssignmentUID = i.AssignmentUID
LEFT OUTER JOIN
MSP_EpmResource_UserView ON MSP_EpmAssignment_UserView.ResourceUID = MSP_EpmResource_UserView.ResourceUID
LEFT OUTER JOIN
MSP_EpmProject_UserView ON MSP_EpmAssignment_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
LEFT OUTER JOIN
MSP_EpmTask_UserView ON MSP_EpmAssignment_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID
我的 table 有以下 Microsoft SQL 服务器触发器:
-- Insert statements for trigger here
DECLARE @AssignmentUID uniqueidentifier;
DECLARE @ResourceUID uniqueidentifier;
DECLARE @ResourceName nvarchar(255);
DECLARE @ResourceClaimsAccount nvarchar(255);
DECLARE @ProjectUID uniqueidentifier;
DECLARE @ProjectName nvarchar(255);
DECLARE @ProjectUrl nvarchar(1000);
DECLARE @TaskUID uniqueidentifier;
DECLARE @TaskName nvarchar(255);
DECLARE @TaskUrl nvarchar(1000);
DECLARE @UserId nvarchar(255);
DECLARE @PWAUrl nvarchar(255);
DECLARE @sql varchar(8000)
SELECT @AssignmentUID = INSERTED.AssignmentUID
FROM INSERTED;
SELECT
@TaskUID = MSP_EpmAssignment_UserView.TaskUID,
@TaskName = MSP_EpmTask_UserView.TaskName,
@ProjectUID = MSP_EpmAssignment_UserView.ProjectUID,
@ProjectName = MSP_EpmProject_UserView.ProjectName,
@ProjectUrl = MSP_EpmProject_UserView.ProjectWorkspaceInternalHRef,
@ResourceUID = MSP_EpmAssignment_UserView.ResourceUID,
@ResourceClaimsAccount = MSP_EpmResource_UserView.UserClaimsAccount,
@ResourceName = MSP_EpmResource_UserView.ResourceName
FROM
MSP_EpmAssignment_UserView
LEFT OUTER JOIN
MSP_EpmResource_UserView ON MSP_EpmAssignment_UserView.ResourceUID = MSP_EpmResource_UserView.ResourceUID
LEFT OUTER JOIN
MSP_EpmProject_UserView ON MSP_EpmAssignment_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
LEFT OUTER JOIN
MSP_EpmTask_UserView ON MSP_EpmAssignment_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID
WHERE
(MSP_EpmAssignment_UserView.AssignmentUID = @AssignmentUID);
INSERT INTO tablename
(ItemTitle, ParentName, ParentUrl, Loginname, SourceId, CreatedAt)
VALUES (@TaskName, @ProjectName, @ProjectUrl, RIGHT(@ResourceClaimsAccount, CHARINDEX('\', REVERSE('\' + @ResourceClaimsAccount)) - 1), 1, GETDATE());
如何在执行 INSERT INTO 语句之前检查@AssignmentUID AND @TaskUID 是否为 NOT NULL?是否还有优化此触发器的潜力?
IF (@AssignmentUID IS NOT NULL AND @TaskUID IS NOT NULL)
BEGIN
...
END
您需要以 set-based 的方式编写触发器,以处理 Inserted
table 可能包含多行的事实。
试试这个代码 - 它使用你的核心 SELECT
并对你试图在开始。这将立即处理 Inserted
table 中包含的 所有 行 - 没有循环,没有游标 - 不需要这样混乱的代码!
INSERT INTO tablename (ItemTitle, ParentName, ParentUrl, Loginname, SourceId, CreatedAt)
SELECT
MSP_EpmTask_UserView.TaskName,
MSP_EpmProject_UserView.ProjectName,
MSP_EpmProject_UserView.ProjectWorkspaceInternalHRef,
RIGHT(MSP_EpmResource_UserView.UserClaimsAccount, CHARINDEX('\', REVERSE('\' + MSP_EpmResource_UserView.UserClaimsAccount)) - 1,
1,
SYSDATETIME()
FROM
MSP_EpmAssignment_UserView
INNER JOIN
Inserted i ON MSP_EpmAssignment_UserView.AssignmentUID = i.AssignmentUID
LEFT OUTER JOIN
MSP_EpmResource_UserView ON MSP_EpmAssignment_UserView.ResourceUID = MSP_EpmResource_UserView.ResourceUID
LEFT OUTER JOIN
MSP_EpmProject_UserView ON MSP_EpmAssignment_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
LEFT OUTER JOIN
MSP_EpmTask_UserView ON MSP_EpmAssignment_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID