Using SQL JOIN and UNION together
Can you try replacing:
LEFT JOIN TranHeader AS b ON
b.TranID = a.TranID
WHERE a.TranRemark1 = @RemarkCode;
with:
LEFT JOIN
( SELECT DISTINCT
TranId, ClientName
FROM TranHeader
) AS b ON
b.TranID = a.TranID
WHERE a.TranRemark1 = @RemarkCode;
How about
SELECT
b.ClientName,
a.TranID,
a.TranRemark1,
a.TranDateOfService,
a.TranPayment
WHERE a.TranRemark1 = @RemarkID JOIN TranHeader b ON b.TranID = a.TranID
UNION ALL
SELECT
b.ClientName,
a.TranID
a.TranRemark2,
a.TranDateOfService,
a.TranPayment
WHERE a.TranRemark2 = @RemarkID JOIN TranHeader b ON b.TranID = a.TranID
UNION ALL
SELECT
b.ClientName,
a.TranID,
a.TranRemark3,
a.TranDateOfService,
a.TranPayment
WHERE a.TranRemark3 = @RemarkID JOIN TranHeader b ON b.TranID = a.TranID
?
I initially suggested
SELECT
b.ClientName,
a.TranID,
a.TranRemark1,
a.TranDateOfService,
a.TranPayment,
a.TranRemark1,
a.TranRemark2,
a.TranRemark3
FROM
TranDetail a JOIN TranHeader As b ON
b.TranID = a.TranID
WHERE a.TranRemark1 = @RemarkCode
OR a.TranRemark2 = @RemarkCode
OR a.TranRemark3 = @RemarkCode;
but thought you probably want a separate line for each remark?