Basically, the idea is this:
'Unroll' tblLossClaims
and return two columns: a tblLossClaims
key (you didn't mention any, so I guess it's going to be LossPolicyID
) and Item
= a single item from LossPolicyID
.
Find matches of unrolled.Item
in tblAutoPolicyList.PolicyIDList
.
Find matches of distinct matched.LossPolicyID
in tblLossClaims.LossPolicyID
.
Update tblLossClaims.PolicyReview
accordingly.
The main UPDATE can look like this:
UPDATE claims
SET PolicyReview = 'Y'
FROM tblLossClaims claims
JOIN (
SELECT DISTINCT unrolled.LossPolicyID
FROM (
SELECT LossPolicyID, Item = itemof(LossPolicyID)
FROM unrolling_join
) unrolled
JOIN tblAutoPolicyList
ON unrolled.ID = tblAutoPolicyList.PolicyIDList
) matched
ON matched.LossPolicyID = claims.LossPolicyID
You can take advantage of the fixed item width and the fixed list format and thus easily split LossPolicyID
without a UDF. I can see this done with the help of a number table and SUBSTRING()
. unrolling_join
in the above query is actually tblLossClaims
joined with the number table.
Here's the definition of unrolled
'zoomed in':
...
(
SELECT LossPolicyID,
Item = SUBSTRING(LossPolicyID,
(v.number - 1) * @ItemLength + 1,
@ItemLength)
FROM tblLossClaims c
JOIN master..spt_values v ON v.type = 'P'
AND v.number BETWEEN 1 AND (LEN(c.LossPolicyID) + 2) / (@ItemLength + 2)
) unrolled
...
master..spt_values
is a system table that is used here as the number table. Filter v.type = 'P'
gives us a rowset with number values from 0 to 2047, which is narrowed down to the list of numbers from 1 to the number of items in LossPolicyID
. Eventually v.number
serves as an array index and is used to cut out single items.
@ItemLength is of course simply LEN(tblAutoPolicyList.PolicyIDList)
. I would probably also declared @ItemLength2 = @ItemLength + 2
so it wasn't calculated every time when applying the filter.
Basically, that's it, if I haven't missed anything.