I'm getting duplicated SQL Server Execution Times from SET STATISTICS IO,TIME ON with dynamic queries. Is this a normal behavior?
You get an output for each statement. The query in
@sql is one statement that gives you the first output and calling
exec is a statement of its own and outputs duration for all the statements in
So if you have two statements in
@sql you get three outputs.
exec (N'select * from dbo.YourTable; select * from dbo.YourTable;');
Output with set statistics time on:
(74 rows affected) SQL Server Execution Times: CPU time = 16 ms, elapsed time = 164 ms. (74 rows affected) SQL Server Execution Times: CPU time = 15 ms, elapsed time = 144 ms. SQL Server Execution Times: CPU time = 31 ms, elapsed time = 309 ms.