Good Day
All we are trying to do is inside a trigger make sure the user is not inserting two fees that have ‘alone’ in the name. Those fees need to be handled individually.
For some reason, it appears the top section of sql quit working two weeks ago. To get around it I recoded it the second way and get the correct results. What I am confused is why would the first portion seemed to have worked for the last several years and now it does not?
SELECT @AloneRecordCount = count(*)
FROM inserted i
INNER JOIN deleted d on i.id = d.id
WHERE i.StatusID = 32
AND d.StatusID <> 32
AND i.id IN
(SELECT settlementid FROM vwFundingDisbursement fd
WHERE fd.DisbTypeName LIKE '%Alone'
AND fd.PaymentMethodID = 0)
SELECT @AloneRecordCount = count(i.id)
FROM inserted i INNER JOIN
deleted d on i.id = d.id
JOIN vwFundingDisbursement fd on i.id = fd.settlementid
WHERE i.StatusID = 32
AND d.StatusID <> 32
AND fd.DisbTypeName like '%Alone'
AND fd.PaymentMethodID = 0
this is on SQL Server 2005
there is no error, instead the top statement will only return 1 or zero
while the bottom statement will return the actual number found.
A schema (or at least how the view is created) would help, but here’s a guess…
If you are looking for multiple rows in vwFundingDisbursement with the value “Alone” in the distribution type name, then a JOIN is going to return multiple rows as your source table (INSERTED) joins to multiple rows in the view. If you use IN though, SQL doesn’t care if it returns multiple matches, it’s only going to give you one row.
As an example:
On a side note, burying a column inside of another column like this is a violation of the normalized form and just asking for problems. Performing this kind of business logic in a trigger is also a dangerous path to go down as you’re finding out.