Dynamic Search Conditions in T‑SQL

I hate dynamic querys as they are not optimized and a query plan cannot be cached….


Sql = some dyanamic sql string built via if logic…

Exec(sql) – will not cache an query execution plan.

In MS Sql   sp_executesql caches query plans – very cool…

Really though, try to avoid them if possible….


Great read on the details…

Newest version of document:  http://www.sommarskog.se/dyn-search-2008.html

Older version

http://www.sommarskog.se/dyn-search-2005.html – Version for SQL 2005 and Earlier


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s