I have code to generate a unique code for a filed in a SQL Server 2017 table. The entire code block works great without any issues. Due to user requirements I have to do this differently now. As you can see from my code it will generate a code for each row of data and set all rows 'Valid' filed to valid. What I absolutely have to do is generate the code for a specific record. And specifically for the most recently updated record. I totally understand how to get the most recently updated records and how to apply IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY but I wondered if there is something similar for recently Updated records. When I say updated record an Approver in Finance will change the Finance_Approval field status 'approved'. That specific record that was just changed to 'approved' is the one I want to generate my code againt. This code: UPDATE Req_submitted Set approved_code = (SELECT FLOOR(RAND()*(525885-15+9)+16458)), Valid = 'valid'
Below is all of code for the Trigger. I'm hoping this is a challenge someone wants to take on as my SQL has hit a wall on this one but the feature is very important.
ALTER TRIGGER [dbo].[Approve_request_trig]
ON [dbo].[Req_submitted] AFTER update AS Begin declare @req_submitted_key int, @Submitted_to_finance_approver_email varchar(50), @approved_denied varchar(50), @approved_finance varchar(50)
select @req_submitted_key = s.req_submitted_key,
@Submitted_to_finance_approver_email = s.Submitted_to_finance_approver_email,
@approved_denied = s.approved_denied,
@approved_finance = s.approved_finance
from inserted s;
if update(approved_finance)
UPDATE Req_submitted
Set approved_code =
(SELECT FLOOR(RAND()*(525885-15+9)+16458)),
Valid = 'valid'
;
End