>>You can use dynamic sql
DECLARE @sql nvarchar(4000)
>>SET @sql = 'SELECT * FROM ' + @Table + 'WHERE ...'
>>EXECUTE (@sql)
>>
>
>Sergey,
>
>I was wondering, from your experience, is there much gain in speed if you put the dynamic SQL in a stored procedure, versus, sending this SQL statement to SQL Server through SPT (VFP)?
Dmitry,
SPT code is the same as an ad-hoc query. SQL Server compiles a new query plan for every ad-hoc query. For a complex query with many joined tables, derived tables, unions etc. SQL Server can take 30 seconds or more to compile the query plan. Using a stored procedure in that case is a speed gain because the query plan is cached. For simpler queries, the compilation time is miniscule but not zero. On a high-volume server, every bit of performance counts so I would put as much as possible into stored procedures. On lower-volume servers, the only concern is the response time for the user so you can get away with a more ad-hoc queries.
sp_executesql is the best of both worlds. Query plans from sp_executesql are cached so subsequent calls with very similar queries will use the cached plan and save any compilation time.