So I'm trying to troubleshoot a query we have here and it's driving me mad.
I've stripped it down to this:
SQL
DECLARE@SalesRepIDINT=248DECLARE@SalesRepsTABLE(UserIDINT)INSERTINTO@SalesRepsSELECTISNULL(sr1.UserID,sr.UserID)FROMSalesRepsrWITH(NOLOCK)LEFTJOINUsersccWITH(NOLOCK)ONsr.UserID=cc.udfCustomerChampionLEFTJOINSalesRepsr1WITH(NOLOCK)ONcc.UserID=sr1.UserIDWHEREsr.SalesRepID=@SalesrepIDSELECTc.Name'Customer',nt.Name'Type'FROMSalesDocumentNotepadsnpWITH(NOLOCK)INNERJOIN@SalesRepssrONsnp.UserID=sr.UserIDLEFTJOINOrderHeaderohWITH(NOLOCK)ONsnp.OrderID=oh.OrderIDLEFTJOINCustomercWITH(NOLOCK)ONoh.CustomerID=c.CustomerIDLEFTJOINNotepadTypentWITH(NOLOCK)onsnp.NotepadTypeID=nt.NotepadTypeIDWHEREsnp.NotepadCaseIDISNULLAND(snp.FollowUpDateTimeISNOTNULL...