Many times we need to run a string formatted
query depend on front end page. For ex..when we need to filter some data from a
table then we can use this concept (by adding sort expression,filter clause etc)
Concept is given, you need to modify as per your requirement
declare
@str1 nvarchar(200)
-----declare a variable
set
@str1='select * from
tablename' --- set your query to variable
exec
sp_executesql
@str1 ----call sql stored procedure sp_executesql
which run a string.