Answer the question
In order to leave comments, you need to log in
How to execute trigger once after update is there update in it too?
Good day everyone!
Help, please, to deal with the trigger ms sql. Trigger on table update.
The table contains a certain Status_id field, data is received into it and at the time of recording Status_id = 2, I need it to be changed to 3 and the procedure was worked out.
As a result, it turns out that if the trigger fires 2 times (it is logical - the moment of recording-updating the melons (via update) and after the update specified in it, but it seemed to me that during the second execution Status_id should be equal to 3. But judging by the fact that the procedure it still works the second time - it is equal to 2, although it will be already 3 by request to the table, because update status_id has already worked.
Below is the full code of the trigger with comments:
CREATE TRIGGER [Upeco Чек-Лист].CreateTask
ON [Upeco Чек-Лист].[ProjectForms_600]
AFTER UPDATE
AS
begin
declare @Id int, @Pid int, @UserId int, @DateCreate datetime,
@C1 int, @C2 int=0, @TaskName nvarchar(max), @TaskSubject nvarchar(max),
@UserUn uniqueidentifier, @DateEnd datetime, @DateBegin datetime, @Img nvarchar(max),
@Status_Id int, @Del int
select @Id=Id, @Pid=[BaseItem_id], @UserId=[Merchandiser_Id], @DateCreate=ISNULL(DateUpdated,DateCreated), @Status_Id=[Status_Id], @Del=isnull([Deleted],0)
from inserted
--ниже предположил, что inserted после update для status_id содержит 2 записи и по этому условие на выборку максимальной
where [Status_Id]=(select max([Status_Id]) from inserted)
select @UserUn = [UserId] from [dbo].[aspnet_UsersData] WITH (NOLOCK) where Id = @UserId
--условие для срабатывания запросов ниже:
IF @Del=0 and @Status_Id=2
begin
select @Img = coalesce(@Img, '<br />') + '<img style="width:400px;" alt="" src="..\..\' + [FileName] + '" /><br /><br />'
from [Application].[FormFieldsDataContent]
where [FormId][email protected] and [FormFieldsDataId]=1388
IF len(@Img)>15
begin
set @DateBegin = GETDATE() + 1
set @DateEnd = GETDATE() + 4
select @TaskName = ISNULL(Poi.Сеть,'') + ', ' + ISNULL(Poi.Область,'') + ', ' + ISNULL(Poi.Город,'') + ', ' + ISNULL(Poi.Адрес,''),
@TaskSubject = coalesce(@TaskSubject + '<br/><br/>', '') + 'Бренд: ' + ISNULL(B.Наименование,'') + ' <br/> Проблема: ' + ISNULL(P.[Описание проблемы],'') + ' <br/> Задача: ' + ISNULL(Z.[Описание проблемы],'')
from [Upeco Чек-Лист].[Список задач на следующий визит_605] Z inner join
[Upeco Чек-Лист].ProjectForms_600 PF on Z.Form_id=PF.Id inner join
GlobalList.Адреска_593 Poi on PF.BaseItem_id=Poi.Id inner join
GlobalList.[Список проблем_603] P on Z.Задача=P.Id left outer join
GlobalList.Бренды_629 B on Z.Бренд=B.Id
where [email protected]
IF len(@TaskSubject)>0
begin
set @TaskSubject = '<p>' + @TaskSubject + '</p>' + @Img
exec [Application].[ProjectTasksAdd]
@ProjectId=63,
@[email protected],
@[email protected],
@[email protected],
@[email protected],
@[email protected],
@[email protected]
end
end
--Обновляем Status_id на 3
update [Upeco Чек-Лист].[ProjectForms_600] set [Status_Id]=3, DateUpdated=GETDATE() where [email protected]
end
end
Answer the question
In order to leave comments, you need to log in
For example like this:
CREATE TRIGGER PublicationAlbumUpdate ON [Album] FOR UPDATE AS
BEGIN
SET NOCOUNT ON
IF NOT (UPDATE ( Publication ) OR UPDATE ( Added )) BEGIN
UPDATE Album SET Publication = GETDATE()
FROM Album INNER JOIN inserted ON Album.ID = inserted.ID
UPDATE Album SET Publication = GETDATE()
FROM Album INNER JOIN inserted ON Album.ID = inserted.EditMaster
END
--
END
--
GO
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question