You don't want to get into cursoring through the resultset row-by-row unless there's no alternative.Here's a set-based approach:Select DT.GUID, DT.CodeName, Case When E.dp_BrandingPrefix Is Null Then 'Fail: Codename not found' When E.dp_GUID = DT.GUID Then 'Pass' Else 'Fail: GUIDs dont match' End As MatchResultFrom ( select a.AttributeValue as GUID, b.CodeName from SKU.dbo.t_DpAttribute a inner join SKU.dbo.t_Dp b on a.DpId = b.DpId where a.DpAttributeKeyId = @attributeid) DTLeft Outer Join ECommerce.dbo.ECM_DistributionPartners As E On ( E.dp_BrandingPrefix = DT.CodeName )