The query concerns one table holding Contacts data. A table valued function is used to return the "status" for each Contact.
The insert statement grabs the unique key from the Contacts table and loads it into a variable called @filteredIdList. This is then joined to the Contacts table and again to the table-valued function by passing the variable @filteredIdList into it.
I have simplified the example down to show how I'm currently returning the status. This needs to be changed into a view instead:
DECLARE @filteredIdList IdList;
INSERT INTO @filteredIdList SELECT ContactID FROM Contacts
SELECT
Contacts.ContactID,
Contact_Status
FROM Contacts
INNER JOIN @filteredIdList [IDLIST] ON Contacts.ContactID = [IDLIST].[id]
INNER JOIN tvfContact_Status(@filteredIdList) Contact_Status
ON [IDLIST].[id] = Contact_Status.ContactID
I have tried creating it as a CTE inside the view but couldn't figure it out.