User People Tagged

This script shows each person in the search results and the name of the current user’s tag they are in. It will also display tags shared with the user.

The recommended name is UserPeopleTagged

Use the following code to Create the SQL Script. See How to create a SQL Script.

SELECT
       t.OwnerName,
       tp.PeopleId ,
       Name = p.Name2 ,
       TagName = t.Name
FROM TagPerson tp
JOIN Tag t ON t.Id = tp.Id
JOIN dbo.People p ON p.PeopleId = tp.PeopleId
JOIN dbo.TagPerson tpq ON tpq.PeopleId = p.PeopleId AND tpq.Id = @qtagid
WHERE       (EXISTS( -- my tags
            SELECT NULL FROM dbo.Users u
            WHERE u.UserId = @userid AND u.PeopleId = t.PeopleId)
         OR EXISTS( -- tags shared with me
            SELECT NULL FROM dbo.Users u
            JOIN dbo.TagShare ts ON ts.PeopleId = u.PeopleId AND ts.TagId = t.Id
            WHERE u.UserId = @userid )
        )
AND TypeId = 1 -- personal tag
ORDER BY p.Name2, t.Name