Tuesday, August 17, 2010

execute dynamic query from stored procedure

After realized the crazy requirements of the site locator program, I know a common store procedure won't work. However, because of our database name is different in each enviorment (staging, QA, prod), the dynamic query in the code won't work either. The only solution is to use sp to run dynamic query. the sql syntax looks like this:

EXECUTE SP_EXECUTESQL @sqlStatement

where @sqlstatement is nvarchar (max 4000 charaters). The caveat here is 4000 is really short, and a long/complex query will easily exceed this limit.

No comments: