A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
If I understand what you want, then
Select
sd.Ordernumber
,MAX(t.LineNumber) Over(Partition By sd.Ordernumber) As Maxln
,t.item
,t.linenumber
FROM #SaleData sd
Join #Test t
ON t.ordernumber = sd.ordernumber
If that does not give you want you want, please show us what you want your query to return given your sample data.
Tom