Click here to Skip to main content
12,698,024 members (25,255 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5.7K views
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralOr you could write a stored procedure and not only have a (o... Pin
John Kasra19-May-11 18:14
memberJohn Kasra19-May-11 18:14 
GeneralReason for my vote of 5 Good example Pin
Amit Kumar Tiwari29-Apr-11 0:59
memberAmit Kumar Tiwari29-Apr-11 0:59 
GeneralBetter for using query plan caching must be: <code> SET @sql... Pin
Gluck28-Apr-11 18:11
memberGluck28-Apr-11 18:11 
GeneralHow does this avoid recreating the query plan? Pin
AspDotNetDev28-Apr-11 15:21
protectorAspDotNetDev28-Apr-11 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 28 Apr 2011
Article Copyright 2011 by reshi999
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid