Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Always use Binding Variables in SQL queries

, 28 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Thanks for that. In MS SQL Server, I use the following technique to avoid full compile:DECLARE @sql VARCHAR(100)DECLARE @pk INTSET @pk = 2SET @sql = 'SELECT id, pcname FROM pod WHERE id = ' + CAST(@pk AS VARCHAR)EXEC (@sql)-- or -- EXEC sp_sqlexec @SQL
Thanks for that. In MS SQL Server, I use the following technique to avoid full compile:
 
DECLARE @sql VARCHAR(100)
DECLARE @pk INT
 
SET @pk = 2
SET @sql = 'SELECT id, pcname FROM pod WHERE id = ' + CAST(@pk AS VARCHAR)
 
EXEC (@sql)
 
-- or -- 

EXEC sp_sqlexec @SQL

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

reshi999

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralOr you could write a stored procedure and not only have a (o... PinmemberJohn Kasra19-May-11 17:14 
GeneralReason for my vote of 5 Good example PinmemberAmit Kumar Tiwari28-Apr-11 23:59 
GeneralBetter for using query plan caching must be: <code> SET @sql... PinmemberGluck28-Apr-11 17:11 
GeneralHow does this avoid recreating the query plan? PinprotectorAspDotNetDev28-Apr-11 14:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 28 Apr 2011
Article Copyright 2011 by reshi999
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid