Step 1:
Create Trigger on Document table:
CREATE TRIGGER TRIG_FINAL_APPLICABILITY_CALC ON document After Update
AS
BEGIN
--*********************************************************************************
--Add to GID Trigger Final Applicability
Update d
Set d.lookup2Id = --Final Applicability
Case
--When Delta Appl = Select One Then Delta Appl Not Evaluated
When (d.lookup9Id = 769 or d.lookup9Id is null) -- 769 = Select One
Then 2475 -- 2355 test: Delta Applicability Not Evaluated
--When Delta Appl = No Change and Ref Appl <> Select One Then Ref Appl
When (d.lookup9Id = 2160 and d.lookup4Id <> 769) --2160 = No Change and 769 = Select One
Then -- Final = Ref
Case d.lookup4Id
When 772 Then 2480 -- Convert Applicability to Final Applicability Yes
When 777 Then 2481 -- Convert Applicability to Final Applicability Yes-Study
When 770 Then 2477 -- Convert Applicability to Final Applicability No
When 771 Then 2479 -- Convert Applicability to Final Applicability Open
When 971 Then 2476 -- Convert Applicability to Final Applicability Deprecated
When 2160 Then 2478 -- Convert Applicability to Final Applicability Not Valid
End
--When Delta Appl = No Change and Ref Appl = Select One Then Ref Appl Not Evaluated – Not Valid
When (d.lookup9Id = 2160 and d.lookup4Id = 769) --2160 = No Change and 769 = Select One
Then 2478 -- 2348 in Test: Reference Applicability Not Evaluated – Not Valid
--Else = Delta Applicability
Else
Case d.lookup9Id
When 772 Then 2480 -- Convert Applicability to Final Applicability Yes
When 777 Then 2481 -- Convert Applicability to Final Applicability Yes-Study
When 770 Then 2477 -- Convert Applicability to Final Applicability No
When 771 Then 2479 -- Convert Applicability to Final Applicability Open
When 971 Then 2476 -- Convert Applicability to Final Applicability Deprecated
End
End
from document d
JOIN inserted i ON d.id = i.id
join documenttype dt on dt.id = d.documentTypeId
join documenttypefielddefinition dtfd on dtfd.documentTypeId = dt.id
where d.active = 'T'
and dtfd.lookupTypeId = 553 --515 in test:only update item types that use Final Applicabilyt Pick List
END
Step 2:
Execute Update queries:
***************************************************************************************************************************************
Update Query #1
***************************************************************************************************************************************
Update d
set d.globalId = d.remainingEstimate
from document d
JOIN documenttypefielddefinition dfd on d.documentTypeId = dfd.documentTypeId
JOIN documentfield df on df.id = dfd.documentFieldId
join documenttype dt on dt.id = d.documentTypeId
where d.remainingEstimate is not null
AND ((df.id = 14 and (dt.documentTypeCategoryId is null or dt.documentTypeCategoryId = 1009))
or (dt.documentTypeCategoryId = 1004 and df.id = 1)
or (d.documentTypeId = 93))
Update Query #2
***************************************************************************************************************************************
Update d
Set d.globalId = '1' + cast(d.id as varchar(40))
--select d.id, d.remainingEstimate, d.globalId, p.name, dt.display
from document d
join project p on p.id = d.projectId
join documenttype dt on dt.id = d.documentTypeId
where d.remainingEstimate is not null
and d.globalId is null
***************************************************************************************************************************************
Update Query #3
***************************************************************************************************************************************
Update d
Set d.globalId = '1' + cast(d.id as varchar(40))
--select d.id, d.remainingEstimate, d.globalId, p.name, dt.display
--select count(*)
from document d
join project p on p.id = d.projectId
join documenttype dt on dt.id = d.documentTypeId
where d.globalId is null
When I run the step1, creating trigger query, it executed successfully.
Next step, is to run all the above 3 update queries, but the update queries are executing with errors.
I’m getting the following error:
Msg 217, Level 16, State 1, Procedure trg_GID_afterUpdate, Line 6
Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).
I couldn’t figure out the problem. Can anyone help me with this query.