Lynn Pettis (another long time poster around here) pointed out there is a LOT more wrong with your query. I didn't bother to look past the hideous formatting but he ran it through a formatter. Once I did, my eyes started bleeding.
SELECT [Build1].dbo.TBL_TYPE_DEF.TYPE_NAME
,[Build1].dbo.TBL_INSTANCES.DOC_NAME
,[Build1].dbo.TBL_INSTANCES.DOC
,[Build1].dbo.TBL_INSTANCES.SEC
,[Build1].dbo.TBL_INSTANCES.VER
,[Build1].dbo.TBL_RELATIONSHIPS.JUSTIFICATION
FROM [Build1].dbo.TBL_INSTANCES
INNER JOIN [Build1].dbo.TBL_TYPE_DEF ON [Build1].dbo.TBL_INSTANCES.TYPE_ID = [Build1].dbo.TBL_TYPE_DEF.TYPE_ID
LEFT JOIN [Build1].dbo.TBL_RELATIONSHIPS ON [Build1].dbo.TBL_INSTANCES.DOC = [Build1].dbo.TBL_RELATIONSHIPS.PARENT_DOC
AND [Build1].dbo.TBL_INSTANCES.SEC = [Build1].dbo.TBL_RELATIONSHIPS.PARENT_SEC
AND [Build1].dbo.TBL_INSTANCES.VER = [Build1].dbo.TBL_RELATIONSHIPS.PARENT_VER
WHERE (
([Build1].dbo.TBL_INSTANCES.DOC + [Build1].dbo.TBL_INSTANCES.SEC + ISNULL(CONVERT(VARCHAR(10), [Build1].dbo.TBL_INSTANCES.VER), '')) NOT IN (
SELECT PARENT_DOC + PARENT_SEC + ISNULL(CONVERT(VARCHAR(10), PARENT_VER), '')
FROM [Build1].dbo.TBL_RELATIONSHIPS AS TBL_RELATIONSHIPS_1
WHERE (
(PARENT_DOC + PARENT_SEC + ISNULL(CONVERT(VARCHAR(10), PARENT_VER), '')) IN (
SELECT DOC + SEC + ISNULL(CONVERT(VARCHAR(10), VER), '')
FROM [Build1].dbo.TBL_INSTANCES AS TBL_INSTANCES_1
WHERE (
TYPE_ID IN (
SELECT TYPE_ID
FROM [Build1].dbo.TBL_TYPE_DEF AS TBL_TYPE_DEF_1
WHERE (
TYPE_ID NOT IN (
SELECT CHILD_TYPE
FROM [Build1].dbo.TBL_RELATIONSHIPS_ALLOWED
)
)
)
)
)
)
AND (CHILD_DOC IS NOT NULL)
)
)
AND (
[Build1].dbo.TBL_INSTANCES.TYPE_ID IN (
SELECT TYPE_ID
FROM [Build1].dbo.TBL_TYPE_DEF AS TBL_TYPE_DEF_1
WHERE (
TYPE_ID NOT IN (
SELECT CHILD_TYPE
FROM [Build1].dbo.TBL_RELATIONSHIPS_ALLOWED AS TBL_RELATIONSHIPS_ALLOWED_1
)
)
)
)
ORDER BY [Build1].dbo.TBL_TYPE_DEF.TYPE_ID
,DOC
,SEC
,VER
You need to use table aliases. IIRC using the full qualification in the select list has been deprecated. Just using aliases would increase the readability a lot. Then there are all the subqueries and column concatenation. You also need to avoid using sql keywords as object or column names. It makes things a lot tougher to work with.
The indexes and stats I mentioned are crucial for this to have a chance, but you could greatly improve this query. It probably needs a complete rewrite.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/