0

enter image description here

I'm getting duplicated SQL Server Execution Times from SET STATISTICS IO,TIME ON with dynamic queries. Is this a normal behavior?

1
  • 1
    You could put SET STATISTICS inside the dynamic batch May 21 '21 at 11:30
2

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 @sql.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.