T-SQL Stored Procedure Coding Guidelines

Transact-SQL (T-SQL) stored procedures should be treated just like reusable application code. You should follow these suggested guidelines to ensure that your stored procedures are solid and robust:

• Check all parameters for valid values and return an error message if a problem exists.

• Be sure that the parameter data types match the column data types they are compared against to avoid data type mismatches and poor query optimization.

• Check the @@error system function after each SQL statement, especially insert, update, and delete, to verify that the statements executed successfully. Return a status code other than 0 if a failure occurs.

• Be sure to comment your code so that when you or others have ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.