这是我的代码我在触发器中第二次更新后变老了StartDateUtc和EndDateUtc
第一次当我更新我的价值时,它的工作正常,但第二次它的旧价值 请给我一些建议
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[NewsUpdateTrigger]'))
DROP TRIGGER [dbo].[NewsUpdateTrigger]
GO
CREATE TRIGGER [dbo].[NewsUpdateTrigger]
ON [dbo].[News]
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @EntityId INT
DECLARE @EntityName varchar(Max)
DECLARE @IsPublish bit
DECLARE @LanguageId int
DECLARE @StoreId int
DECLARE @InTime Datetime
SELECT @EntityName='News'
DECLARE @StatDate DateTime
DECLARE @EndDate DateTime
SELECT @EntityId = DELETED.Id FROM DELETED
SELECT @IsPublish = DELETED.Published FROM DELETED
SELECT @StatDate= DELETED.StartDateUtc FROM DELETED
SELECT @EndDate= DELETED.EndDateUtc FROM DELETED
IF EXISTS (SELECT * FROM Incremental_Solr_Custom where EntityName='News' and EntityId = @EntityId)
BEGIN
--If StartDateUtc and EndDateUtc expire Manually Set the is Publish=False
IF EXISTS (SELECT * FROM DELETED WHERE StartDateUtc NOT BETWEEN Cast( GETDate() as date) AND Cast( GETDate() as date)
AND Cast( GETDate() as date) NOT BETWEEN StartDateUtc AND EndDateUtc )
BEGIN
UPDATE Incremental_Solr_Custom SET SolrStatus =1,IsPublish=0 WHERE EntityId = @EntityId AND EntityName='News';
Insert into TriggerTesterTable(ErrorMethodName)values(' If StartDateUtc and EndDateUtc expire Set the is Publish=False '+ cast( @StatDate as varchar) + ' '+cast( @EndDate as varchar))
END
--StartDate and EndDate is Not expire Get From News Table
IF EXISTS (SELECT * FROM DELETED WHERE StartDateUtc BETWEEN Cast( GETDate() as date) AND Cast( GETDate() as date)
OR Cast( GETDate() as date) BETWEEN StartDateUtc AND EndDateUtc)
BEGIN
SELECT @IsPublish = DELETED.Published FROM DELETED
UPDATE Incremental_Solr_Custom SET SolrStatus =1,IsPublish=@IsPublish WHERE EntityId = @EntityId AND EntityName='News';
Insert into TriggerTesterTable(ErrorMethodName)values('StartDate and EndDate is Not expire Get IsPublish from News '+cast( @StatDate as varchar) + ' '+cast( @EndDate as varchar));
END
END
END
答案 0 :(得分:0)
我认为您正在创建更新触发器,但您正在使用DELETED表(也称为魔术表)而不是INSERTED表。 请参阅此链接,它可能对您有所帮助https://www.mindstick.com/Articles/12187/use-of-dml-dll-and-logon-trigger-in-sql
此外,你可以使用'而不是触发'的概念,因为插入是某些条件的基础。
答案 1 :(得分:0)
使用此SELECT * FROM INSTEAD放置SELECT * FROM DELETED