I have a table with category codes which listed in another table, there are 8 category codes and currently I'm doing the below (below is massively simplified):
SELECT HeaderCode
, C1.CategoryName AS C1
, C2.CategoryName AS C2
, C3.CategoryName AS C3
, C4.CategoryName AS C4
, C5.CategoryName AS C5
, C6.CategoryName AS C6
, C7.CategoryName AS C7
, C8.CategoryName AS C8
FROM Header H
INNER JOIN Cats C1 ON H.Cat1 = C1.CategoryID
INNER JOIN Cats C2 ON H.Cat2 = C2.CategoryID
INNER JOIN Cats C3 ON H.Cat3 = C3.CategoryID
INNER JOIN Cats C4 ON H.Cat4 = C4.CategoryID
INNER JOIN Cats C5 ON H.Cat5 = C5.CategoryID
INNER JOIN Cats C6 ON H.Cat6 = C6.CategoryID
INNER JOIN Cats C7 ON H.Cat7 = C7.CategoryID
INNER JOIN Cats C8 ON H.Cat8 = C8.CategoryID
I did think about a function to get the data but it would be slow as there are 200,000+ records I need to get this information for.
Performance is so far fine but there are times I have to use dynamic SQL and doing this with another 5+ joins is a big mess.
Is there a better/more convenient/easier to maintain way to do this?