I have this query:
SELECT B.IMAGE_ID as image_id_fav,I.Image_Path as image_path_fav
FROM Buddies B
INNER JOIN @favDT F ON F.favorite_id = B.Reg_ID and b.favorite_id=@regID
INNER JOIN Images I ON I.Image_ID = B.Image_ID
where b.Image_ID >0
union all
select I.image_id as image_id_fav,I.Image_Path as image_path_fav FROM Buddies B
inner join @favDT F ON F.favorite_id = B.Reg_ID and b.favorite_id=@regID
inner join registration R on R.Reg_ID = F.favorite_id
INNER JOIN Images I ON R.Default_Image = I.Image_ID
WHERE B.Image_ID=0
union all
SELECT I.IMAGE_ID as image_id_fav, I.IMAGE_PATH as image_path_fav FROM @favDT F
LEFT OUTER JOIN Buddies B ON B.Reg_ID = F.favorite_id and b.favorite_id=@regID
INNER JOIN registration R on R.Reg_ID =F.favorite_id
INNER JOIN Images I ON R.Default_Image = I.Image_ID
WHERE B.Reg_ID IS NULL
i need to order by the F.favorite_id but i keep getting incorrect syntax near the keyword union all