I have a function but it runs very slow, I think the reason is because it has Order by case statement.
Can anyone help to restructure this query , that may runs faster but the same result.
The table stu_assignemnt_address doesn't have house modifier, but the masterAddress table has it.
Create Function Get_AddressID (@i_Pupil_Number int)
Returns int
Begin
Declare @AddressID int
SELECT @AddressID= add.address_id
From address add inner join stud_assignment_address saa on add.street_number = saa.street_number
And add.full_street_name = saa.street_name
And add.postal_code = saa.postal_code
Where add.pupil_number = i_pupil_number
Order by CASE WHEN add.housemodifier IS NULL THEN '0' ELSE 'Z' END, add.x_coordinate, add.y_coordinate
Return @AddressID
END
SQLFriend