T-SQL: Prudent use of Select ... As
Every database developer using T-SQL will agree that the Select ... As feature allows one to easily grab values into a variable. However, after I got my fingers burnt recently, I had to share this to remind everyone how important it is to exercise prudence with its usage. Given the table below, assuming its an SQL table named Students. Id Name Age Class 1 Fade Ayomi 10 5 2 Oluwayomi Ojo 9 6 3 Kemi Mide 8 4 And given the SQL statements below which performs certain operations on the table given above declare @ StudentAge int ; select @ StudentAge = Age from Students where Id = 1 ; set @ StudentAge = Age * 2 ; select @ StudentAge = Age from Students where Id = 5 ; select @ StudentAge as AgeSelected; So what would AgeSelected resolve to according to the provided information? Ordinarily, one would expect AgeSelected to be NULL however, since @StudentAge carried a value initially, and the row described in the where clause do not exist in the table, @Stud...