The Daily WTF
Follow
CodeSOD: Stored Procedures are Better
Building SQL queries via string concatenation and sending them to the database poses risks. However, a stored procedure in the text exemplifies how the "sending" part can also be problematic.The stored procedure uses string concatenation to build SQL statements, despite T-SQL's ability to handle variables within SQL statements.This concatenation makes the code vulnerable to SQL injection attacks and reduces its readability and maintainability.The use of @TICK, an empty string set to a single character, appears unnecessary and confusing.Inconsistent capitalization of variables (@SQLSelect and @SQLWHERE) adds to the code's lack of clarity.The code's structure suggests it may have been originally written in ASP and later converted to a stored procedure without significant restructuring.This conversion may have overlooked opportunities to improve performance and security.The new employee, Kris, finds the code perplexing and highlights its potential issues.The stored procedure serves as an example of the importance of considering not only the query construction but also the execution phase when addressing security and code quality in SQL environments.Understanding the capabilities of the database language and applying best practices can help mitigate risks and improve code quality.