I’m just reading my company’s code guidelines and it says to never treat variables as a constant in sql server, use literals instead. The reasoning is that sql server can’t build a good execution plan when you’re using variables in the query.
Anybody know if this is still true? We’re using MSSQL 2005 and this document may have been written for 2000.
This is ‘generally’ true for SQL Server 2005 since it now tries to pre-generate execution plans for as many queries as possible. Your mileage may vary since it depends on your data and your SARG (test your query plans and see) but this tecdoc (they were using a pre-release version of SQL Server 2005) states:
http://www.microsoft.com/technet/prodtechnol/sql/2005/qrystats.mspx