You could try the following query instead of your cursor: WITH cte AS ( SELECT a. Id, b_id = b. Id, rnk = ROW_NUMBER() OVER ( PARTITION BY a.Id ORDER BY ABS(DATEDIFF(SECOND, timestamp, a.
Call_time)) ASC ) FROM inserted a INNER JOIN dbo. Table_b be ON b. Caller = a.
Caller AND b. Timestamp > DATEADD(SECOND, -15, a. Call_time) AND b.
Timestamp Msn + b. Ddi = SUBSTRING(a. Service, 1, LEN(b.
Msn + b. Ddi)) ) UPDATE dbo. Table_a SET table_b_id = cte.
B_id FROM cte WHERE cte. Id = dbo. Table_a.
Id AND cte. Rnk = 1 Maybe there's something I missed, but at least, when I tested, this UPDATE produced the same results as the cursor in your trigger I didn't change your logic too much, but, as a matter of fact, this bit … AND b. Msn + b.
Ddi = SUBSTRING(a. Service, 1, LEN(b. Msn + b.
Ddi)) … might render the query non sargable I would probably split this particular check into two: … AND b. Msn = SUBSTRING(a. Service, 1, LEN(b.
Msn)) AND b. Ddi = SUBSTRING(a. Service, LEN(b.
Msn) + 1, LEN(b. Ddi)) … Useful reading: WITH common_table_expression (Transact-SQL) Ranking Functions (Transact-SQL) ROW_NUMBER (Transact-SQL).
You could try the following query instead of your cursor: WITH cte AS ( SELECT a. Id, b_id = b. Id, rnk = ROW_NUMBER() OVER ( PARTITION BY a.Id ORDER BY ABS(DATEDIFF(SECOND, timestamp, a.
Call_time)) ASC ) FROM inserted a INNER JOIN dbo. Table_b be ON b. Caller = a.
Caller AND b. Timestamp > DATEADD(SECOND, -15, a. Call_time) AND b.
Timestamp Msn + b. Ddi = SUBSTRING(a. Service, 1, LEN(b.
Msn + b. Ddi)) ) UPDATE dbo. Table_a SET table_b_id = cte.
B_id FROM cte WHERE cte. Id = dbo. Table_a.
Id AND cte. Rnk = 1 ; Maybe there's something I missed, but at least, when I tested, this UPDATE produced the same results as the cursor in your trigger. I didn't change your logic too much, but, as a matter of fact, this bit … AND b.
Msn + b. Ddi = SUBSTRING(a. Service, 1, LEN(b.
Msn + b. Ddi)) … might render the query non-sargable. I would probably split this particular check into two: … AND b.
Msn = SUBSTRING(a. Service, 1, LEN(b. Msn)) AND b.
Ddi = SUBSTRING(a. Service, LEN(b. Msn) + 1, LEN(b.
Ddi)) … Useful reading: WITH common_table_expression (Transact-SQL) Ranking Functions (Transact-SQL) ROW_NUMBER (Transact-SQL).
Thx for your answer. The problem is that we get wrong data (badformed callerid ...) and we must correct it. Now we correct the insert-process and then we did'nt need the trigger.
Thx for your help. – kockiren Nov 9 at 16:26.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.