SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to construct a delay loop for simulation?

The following Microsoft SQL Server T-SQL scripts can be used to create delay between statements for simulation purposes or slow down batch execution.

------------

-- T-SQL High CPU Load Simulation - SQL Server 100% CPU Usage

------------

-- Run the following script in number-of-CPUs connections

DECLARE @float1 float, @float2 float

WHILE (1=1)

BEGIN

   SET @float1 = SQRT(SQUARE(datepart(ms,getdate())))

   WAITFOR DELAY '00:00:00'

   SET @float2 = SQRT(SQUARE(datepart(ms,getdate())))

END

------------

-- T-SQL delay funcion averaging 10 seconds - T-SQL waitfor

 

DECLARE  @Sec    INT,

         @delay INT

 

SET @delay = 10

 

PRINT convert(varchar, getdate(), 109)   

 

WHILE (2 > 1)

  BEGIN

    SELECT @Sec = datepart(ss,getdate())

    IF (@Sec%@delay = 0)

      BREAK

  END

 

PRINT convert(varchar, getdate(), 109)   

/* Messages

 

Feb 27 2009 11:49:04:607AM

Feb 27 2009 11:49:10:000AM

*/

------------

-- SQL Server waitfor delay usage to slow down T-SQL program

 

-- 1 minute waitfor delay

BEGIN

    SELECT GETDATE()

    WAITFOR DELAY '00:01';

    SELECT GETDATE()

END;

GO

-- 2019-02-27 15:16:27.217

-- 2019-02-27 15:17:27.217
------------

 

-- 11 sec waitfor delay

BEGIN

    SELECT GETDATE()

    WAITFOR DELAY '00:0:11';

    SELECT GETDATE()

END;

GO

-- 2019-02-27 15:21:33.590

-- 2019-02-27 15:21:44.590

------------

Related articles:

SQL SERVER – Delay Function – WAITFOR clause – Delay Execution of Commands

http://www.sqlusa.com/bestpractices2005/hugeupdate/

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.