473,503 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WAITFOR DELAY

System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003
Server
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000
Server

If you run the code below, you'll notice something odd occuring. The
MilliSecond value does not change after a 1Millisecond delay. Is this a bug
or am I doing something wrong?
Any assistance will be greatly appreciated
----------------------------------------------------------------------------
-------
/*
Programmer : Des L. Davis
Date : July 4, 2004
Purpose : Testing Delayed Reaction
*/

CREATE PROCEDURE [sp_TestDelay] AS

DECLARE @DELAYPERIOD NCHAR(12)
SET @DELAYPERIOD = '00:00:00:001'
DECLARE @Retries INT
SET @Retries = 0
DECLARE @MAXTRIES INT
SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
DECLARE @Modified DATETIME

WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN

SET @Modified = GetDate() -- Set Modification Date

PRINT @Retries
PRINT @Modified
PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR, @Modified ) )
PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
@Modified ) )
PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) )
PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE, @Modified ) )
PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND, @Modified ) )
PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND,
@Modified ) )

SET @Retries = @Retries + 1 -- Increment loop counter and retry

WAITFOR DELAY @DELAYPERIOD

END
GO

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

Jul 20 '05 #1
14 18803
The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only
accurate to 3.33 milliseconds and rounding may occur. You can see what is
happening with the following

DECLARE @DELAYPERIOD datetime
SET @DELAYPERIOD = '00:00:00:001'
SELECT @DELAYPERIOD

This is documented in the SQL Server 2000 Books Online:

<Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">

Date and time data from January 1, 1753 through December 31, 9999, to an
accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds
or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007
seconds, as shown in the table.

Example Rounded example
01/01/98 23:59:59.999 1998-01-02 00:00:00.000

01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, or
01/01/98 23:59:59.998

01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
01/01/98 23:59:59.993,
01/01/98 23:59:59.994

01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
01/01/98 23:59:59.991

</Excerpt>

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:yq*********************@read2.cgocable.net...
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003
Server
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000
Server

If you run the code below, you'll notice something odd occuring. The
MilliSecond value does not change after a 1Millisecond delay. Is this a bug or am I doing something wrong?
Any assistance will be greatly appreciated
-------------------------------------------------------------------------- -- -------
/*
Programmer : Des L. Davis
Date : July 4, 2004
Purpose : Testing Delayed Reaction
*/

CREATE PROCEDURE [sp_TestDelay] AS

DECLARE @DELAYPERIOD NCHAR(12)
SET @DELAYPERIOD = '00:00:00:001'
DECLARE @Retries INT
SET @Retries = 0
DECLARE @MAXTRIES INT
SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
DECLARE @Modified DATETIME

WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN

SET @Modified = GetDate() -- Set Modification Date

PRINT @Retries
PRINT @Modified
PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR, @Modified ) )
PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
@Modified ) )
PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) )
PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE, @Modified ) ) PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND, @Modified ) )
PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND,
@Modified ) )

SET @Retries = @Retries + 1 -- Increment loop counter and retry

WAITFOR DELAY @DELAYPERIOD

END
GO

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

Jul 20 '05 #2
I also notice after running a few tests that WAITFOR seems to have a
resolution of 1/100th second.

Specifying a delay of '00:00:00:003' causes the same delay as if I'd
specified 1/100th second.

WAITFOR DELAY '00:00:00:013' causes a 2/100th second delay (:020 or .02), so
it apparently always rounds up.

The behavior seems easier to follow with less output.
alter PROCEDURE [sp_TestDelay] AS
DECLARE @DELAYPERIOD DATETIME
SET @DELAYPERIOD = '00:00:00:013'
DECLARE @Retries INT
SET @Retries = 0
WHILE ( @Retries <= 1000 ) BEGIN
PRINT DATEPART( MILLISECOND, getdate() )
SET @Retries = @Retries + 1 -- Increment loop counter and retry
WAITFOR DELAY @DELAYPERIOD
END
GO

exec sp_testdelay

577
597
617
637
657
.... etc.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:rS*****************@newsread2.news.pas.earthl ink.net...
The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only
accurate to 3.33 milliseconds and rounding may occur. You can see what is
happening with the following

DECLARE @DELAYPERIOD datetime
SET @DELAYPERIOD = '00:00:00:001'
SELECT @DELAYPERIOD

This is documented in the SQL Server 2000 Books Online:

<Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">

Date and time data from January 1, 1753 through December 31, 9999, to an
accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds
or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007
seconds, as shown in the table.

Example Rounded example
01/01/98 23:59:59.999 1998-01-02 00:00:00.000

01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, or
01/01/98 23:59:59.998

01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
01/01/98 23:59:59.993,
01/01/98 23:59:59.994

01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
01/01/98 23:59:59.991

</Excerpt>

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:yq*********************@read2.cgocable.net...
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003
Server
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000
Server

If you run the code below, you'll notice something odd occuring. The
MilliSecond value does not change after a 1Millisecond delay. Is this a bug or am I doing something wrong?
Any assistance will be greatly appreciated
-------------------------------------------------------------------------- -- -------
/*
Programmer : Des L. Davis
Date : July 4, 2004
Purpose : Testing Delayed Reaction
*/

CREATE PROCEDURE [sp_TestDelay] AS

DECLARE @DELAYPERIOD NCHAR(12)
SET @DELAYPERIOD = '00:00:00:001'
DECLARE @Retries INT
SET @Retries = 0
DECLARE @MAXTRIES INT
SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
DECLARE @Modified DATETIME

WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN

SET @Modified = GetDate() -- Set Modification Date

PRINT @Retries
PRINT @Modified
PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR, @Modified ) )
PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
@Modified ) )
PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) )
PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE, @Modified ) ) PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND, @Modified ) )
PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND,
@Modified ) )

SET @Retries = @Retries + 1 -- Increment loop counter and retry

WAITFOR DELAY @DELAYPERIOD

END
GO

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


Jul 20 '05 #3
Standard Windows time API calls are only accurate to about 10 ms and this is
apparently how SQL Server implements WAITFOR DELAY. When I run a Profiler
trace, I see statements that run quickly have a reported duration of either
0 , 13 or 16 milliseconds but nothing in between.

Below is my test script that shows the actual behavior of WAITFOR DELAY.
The bottom line is that SQL Server will wait for at least the specified time
but can wait longer due to the combination of datetime rounding and timer
interval resolution.

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#Metrics') IS NOT NULL DROP TABLE #Metrics
CREATE TABLE #Metrics
(
MillisecondDelay int NOT NULL,
MillisecondDelayTime datetime NOT NULL,
Retry int NOT NULL,
StartTime datetime NOT NULL,
EndTime datetime NOT NULL
)
DECLARE @MillisecondDelay int
DECLARE @MillisecondDelayTime datetime
DECLARE @Retries int
DECLARE @StartTime datetime
DECLARE @EndTime datetime

SET @MillisecondDelay = 0
WHILE @MillisecondDelay < 50
BEGIN
SET @MillisecondDelay = @MillisecondDelay + 1
SET @MillisecondDelayTime = DATEADD(ms, @MillisecondDelay,
'00:00:00:000')
SET @Retries = 0
WHILE @Retries <= 100
BEGIN
SET @Retries = @Retries + 1 -- Increment loop counter and retry
SET @StartTime = getdate()
WAITFOR DELAY @MillisecondDelayTime
SET @EndTime = getdate()
INSERT INTO #Metrics VALUES(@MillisecondDelay,
@MillisecondDelayTime, @Retries, @StartTime, @EndTime)
END
END

SELECT
MillisecondDelayTime,
AVG(ActualMilliseconds) AS ActualAverageMilliseconds
FROM (
SELECT
MillisecondDelayTime,
DATEDIFF(ms, StartTime, EndTime) AS ActualMilliseconds
FROM #Metrics a
WHERE Retry > 1) AS Metrics
GROUP BY
MillisecondDelayTime
ORDER BY
MillisecondDelayTime

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Aaron W. West" <ta******@hotmail.NO.SPAM> wrote in message
news:E7********************@speakeasy.net...
I also notice after running a few tests that WAITFOR seems to have a
resolution of 1/100th second.

Specifying a delay of '00:00:00:003' causes the same delay as if I'd
specified 1/100th second.

WAITFOR DELAY '00:00:00:013' causes a 2/100th second delay (:020 or .02), so it apparently always rounds up.

The behavior seems easier to follow with less output.
alter PROCEDURE [sp_TestDelay] AS
DECLARE @DELAYPERIOD DATETIME
SET @DELAYPERIOD = '00:00:00:013'
DECLARE @Retries INT
SET @Retries = 0
WHILE ( @Retries <= 1000 ) BEGIN
PRINT DATEPART( MILLISECOND, getdate() )
SET @Retries = @Retries + 1 -- Increment loop counter and retry
WAITFOR DELAY @DELAYPERIOD
END
GO

exec sp_testdelay

577
597
617
637
657
... etc.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:rS*****************@newsread2.news.pas.earthl ink.net...
The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only
accurate to 3.33 milliseconds and rounding may occur. You can see what is
happening with the following

DECLARE @DELAYPERIOD datetime
SET @DELAYPERIOD = '00:00:00:001'
SELECT @DELAYPERIOD

This is documented in the SQL Server 2000 Books Online:

<Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">

Date and time data from January 1, 1753 through December 31, 9999, to an
accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or ..007 seconds, as shown in the table.

Example Rounded example
01/01/98 23:59:59.999 1998-01-02 00:00:00.000

01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, or
01/01/98 23:59:59.998

01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
01/01/98 23:59:59.993,
01/01/98 23:59:59.994

01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
01/01/98 23:59:59.991

</Excerpt>

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:yq*********************@read2.cgocable.net...
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003
Server
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000
Server

If you run the code below, you'll notice something odd occuring. The
MilliSecond value does not change after a 1Millisecond delay. Is this a

bug
or am I doing something wrong?
Any assistance will be greatly appreciated


--------------------------------------------------------------------------
--
-------
/*
Programmer : Des L. Davis
Date : July 4, 2004
Purpose : Testing Delayed Reaction
*/

CREATE PROCEDURE [sp_TestDelay] AS

DECLARE @DELAYPERIOD NCHAR(12)
SET @DELAYPERIOD = '00:00:00:001'
DECLARE @Retries INT
SET @Retries = 0
DECLARE @MAXTRIES INT
SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
DECLARE @Modified DATETIME

WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN

SET @Modified = GetDate() -- Set Modification Date

PRINT @Retries
PRINT @Modified
PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR, @Modified ) )
PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
@Modified ) )
PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) )
PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE,

@Modified ) )
PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND, @Modified ) ) PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND,
@Modified ) )

SET @Retries = @Retries + 1 -- Increment loop counter and retry

WAITFOR DELAY @DELAYPERIOD

END
GO


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


Jul 20 '05 #4
Guys,

I appreciate the assistance you've all provided. Any suggested work around?

"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:Mh*****************@newsread2.news.pas.earthl ink.net...
Standard Windows time API calls are only accurate to about 10 ms and this is apparently how SQL Server implements WAITFOR DELAY. When I run a Profiler
trace, I see statements that run quickly have a reported duration of either 0 , 13 or 16 milliseconds but nothing in between.

Below is my test script that shows the actual behavior of WAITFOR DELAY.
The bottom line is that SQL Server will wait for at least the specified time but can wait longer due to the combination of datetime rounding and timer
interval resolution.

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#Metrics') IS NOT NULL DROP TABLE #Metrics
CREATE TABLE #Metrics
(
MillisecondDelay int NOT NULL,
MillisecondDelayTime datetime NOT NULL,
Retry int NOT NULL,
StartTime datetime NOT NULL,
EndTime datetime NOT NULL
)
DECLARE @MillisecondDelay int
DECLARE @MillisecondDelayTime datetime
DECLARE @Retries int
DECLARE @StartTime datetime
DECLARE @EndTime datetime

SET @MillisecondDelay = 0
WHILE @MillisecondDelay < 50
BEGIN
SET @MillisecondDelay = @MillisecondDelay + 1
SET @MillisecondDelayTime = DATEADD(ms, @MillisecondDelay,
'00:00:00:000')
SET @Retries = 0
WHILE @Retries <= 100
BEGIN
SET @Retries = @Retries + 1 -- Increment loop counter and retry
SET @StartTime = getdate()
WAITFOR DELAY @MillisecondDelayTime
SET @EndTime = getdate()
INSERT INTO #Metrics VALUES(@MillisecondDelay,
@MillisecondDelayTime, @Retries, @StartTime, @EndTime)
END
END

SELECT
MillisecondDelayTime,
AVG(ActualMilliseconds) AS ActualAverageMilliseconds
FROM (
SELECT
MillisecondDelayTime,
DATEDIFF(ms, StartTime, EndTime) AS ActualMilliseconds
FROM #Metrics a
WHERE Retry > 1) AS Metrics
GROUP BY
MillisecondDelayTime
ORDER BY
MillisecondDelayTime

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Aaron W. West" <ta******@hotmail.NO.SPAM> wrote in message
news:E7********************@speakeasy.net...
I also notice after running a few tests that WAITFOR seems to have a
resolution of 1/100th second.

Specifying a delay of '00:00:00:003' causes the same delay as if I'd
specified 1/100th second.

WAITFOR DELAY '00:00:00:013' causes a 2/100th second delay (:020 or ..02),
so
it apparently always rounds up.

The behavior seems easier to follow with less output.
alter PROCEDURE [sp_TestDelay] AS
DECLARE @DELAYPERIOD DATETIME
SET @DELAYPERIOD = '00:00:00:013'
DECLARE @Retries INT
SET @Retries = 0
WHILE ( @Retries <= 1000 ) BEGIN
PRINT DATEPART( MILLISECOND, getdate() )
SET @Retries = @Retries + 1 -- Increment loop counter and retry
WAITFOR DELAY @DELAYPERIOD
END
GO

exec sp_testdelay

577
597
617
637
657
... etc.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:rS*****************@newsread2.news.pas.earthl ink.net...
The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only
accurate to 3.33 milliseconds and rounding may occur. You can see what is happening with the following

DECLARE @DELAYPERIOD datetime
SET @DELAYPERIOD = '00:00:00:001'
SELECT @DELAYPERIOD

This is documented in the SQL Server 2000 Books Online:

<Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">

Date and time data from January 1, 1753 through December 31, 9999, to an
accuracy of one three-hundredth of a second (equivalent to 3.33

milliseconds
or 0.00333 seconds). Values are rounded to increments of .000, .003, or

.007
seconds, as shown in the table.

Example Rounded example
01/01/98 23:59:59.999 1998-01-02 00:00:00.000

01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, or
01/01/98 23:59:59.998

01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
01/01/98 23:59:59.993,
01/01/98 23:59:59.994

01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
01/01/98 23:59:59.991

</Excerpt>

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:yq*********************@read2.cgocable.net...
System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003
Server
Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000
Server

If you run the code below, you'll notice something odd occuring. The
MilliSecond value does not change after a 1Millisecond delay. Is this a
bug
or am I doing something wrong?
Any assistance will be greatly appreciated


--------------------------------------------------------------------------
--
-------
/*
Programmer : Des L. Davis
Date : July 4, 2004
Purpose : Testing Delayed Reaction
*/

CREATE PROCEDURE [sp_TestDelay] AS

DECLARE @DELAYPERIOD NCHAR(12)
SET @DELAYPERIOD = '00:00:00:001'
DECLARE @Retries INT
SET @Retries = 0
DECLARE @MAXTRIES INT
SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
DECLARE @Modified DATETIME

WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN

SET @Modified = GetDate() -- Set Modification Date

PRINT @Retries
PRINT @Modified
PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR,

@Modified ) ) PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
@Modified ) )
PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) ) PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE,

@Modified ) )
PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND,

@Modified ) ) PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND, @Modified ) )

SET @Retries = @Retries + 1 -- Increment loop counter and retry

WAITFOR DELAY @DELAYPERIOD

END
GO


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



Jul 20 '05 #5
Des L. Davis (de*******@cogeco.ca) writes:
I appreciate the assistance you've all provided. Any suggested work
around?


Maybe. If you tell us what the actual business problem you are trying
to solve, we might come up with something. Trying to run a waitfor in
only 1 ms, when there is a 10 ms accuracy sounds like a dead end to me.

OK, so you could write an extended stored procedure to do the waiting,
but if the Windows API only gives you 10 ms, you would have to roll your
own, which does not sound trivial to me.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #6
Erland's extended proc idea is interesting but, in addition the complexity,
there may be some overhead involved. Perhaps you can describe the business
problem. Perhaps there are better methods besides SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:bH*******************@read1.cgocable.net...
Guys,

I appreciate the assistance you've all provided. Any suggested work around?
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:Mh*****************@newsread2.news.pas.earthl ink.net...
Standard Windows time API calls are only accurate to about 10 ms and this
is
apparently how SQL Server implements WAITFOR DELAY. When I run a Profiler trace, I see statements that run quickly have a reported duration of either
0 , 13 or 16 milliseconds but nothing in between.

Below is my test script that shows the actual behavior of WAITFOR DELAY.
The bottom line is that SQL Server will wait for at least the specified

time
but can wait longer due to the combination of datetime rounding and timer interval resolution.

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#Metrics') IS NOT NULL DROP TABLE #Metrics
CREATE TABLE #Metrics
(
MillisecondDelay int NOT NULL,
MillisecondDelayTime datetime NOT NULL,
Retry int NOT NULL,
StartTime datetime NOT NULL,
EndTime datetime NOT NULL
)
DECLARE @MillisecondDelay int
DECLARE @MillisecondDelayTime datetime
DECLARE @Retries int
DECLARE @StartTime datetime
DECLARE @EndTime datetime

SET @MillisecondDelay = 0
WHILE @MillisecondDelay < 50
BEGIN
SET @MillisecondDelay = @MillisecondDelay + 1
SET @MillisecondDelayTime = DATEADD(ms, @MillisecondDelay,
'00:00:00:000')
SET @Retries = 0
WHILE @Retries <= 100
BEGIN
SET @Retries = @Retries + 1 -- Increment loop counter and retry SET @StartTime = getdate()
WAITFOR DELAY @MillisecondDelayTime
SET @EndTime = getdate()
INSERT INTO #Metrics VALUES(@MillisecondDelay,
@MillisecondDelayTime, @Retries, @StartTime, @EndTime)
END
END

SELECT
MillisecondDelayTime,
AVG(ActualMilliseconds) AS ActualAverageMilliseconds
FROM (
SELECT
MillisecondDelayTime,
DATEDIFF(ms, StartTime, EndTime) AS ActualMilliseconds
FROM #Metrics a
WHERE Retry > 1) AS Metrics
GROUP BY
MillisecondDelayTime
ORDER BY
MillisecondDelayTime

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Aaron W. West" <ta******@hotmail.NO.SPAM> wrote in message
news:E7********************@speakeasy.net...
I also notice after running a few tests that WAITFOR seems to have a
resolution of 1/100th second.

Specifying a delay of '00:00:00:003' causes the same delay as if I'd
specified 1/100th second.

WAITFOR DELAY '00:00:00:013' causes a 2/100th second delay (:020 or .02),
so
it apparently always rounds up.

The behavior seems easier to follow with less output.
alter PROCEDURE [sp_TestDelay] AS
DECLARE @DELAYPERIOD DATETIME
SET @DELAYPERIOD = '00:00:00:013'
DECLARE @Retries INT
SET @Retries = 0
WHILE ( @Retries <= 1000 ) BEGIN
PRINT DATEPART( MILLISECOND, getdate() )
SET @Retries = @Retries + 1 -- Increment loop counter and retry
WAITFOR DELAY @DELAYPERIOD
END
GO

exec sp_testdelay

577
597
617
637
657
... etc.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:rS*****************@newsread2.news.pas.earthl ink.net...
The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only
accurate to 3.33 milliseconds and rounding may occur. You can see what is happening with the following

DECLARE @DELAYPERIOD datetime
SET @DELAYPERIOD = '00:00:00:001'
SELECT @DELAYPERIOD

This is documented in the SQL Server 2000 Books Online:

<Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">

Date and time data from January 1, 1753 through December 31, 9999, to
an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds
or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007
seconds, as shown in the table.

Example Rounded example
01/01/98 23:59:59.999 1998-01-02 00:00:00.000

01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
01/01/98 23:59:59.996,
01/01/98 23:59:59.997, or
01/01/98 23:59:59.998

01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
01/01/98 23:59:59.993,
01/01/98 23:59:59.994

01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
01/01/98 23:59:59.991

</Excerpt>

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:yq*********************@read2.cgocable.net...
> System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
> Software: Microsoft SQL Server 2000 Enterprise running on Windows

2003 > Server
> Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000 > Server
>
> If you run the code below, you'll notice something odd occuring. The > MilliSecond value does not change after a 1Millisecond delay. Is this a bug
> or am I doing something wrong?
> Any assistance will be greatly appreciated


--------------------------------------------------------------------------
--
> -------
> /*
> Programmer : Des L. Davis
> Date : July 4, 2004
> Purpose : Testing Delayed Reaction
> */
>
> CREATE PROCEDURE [sp_TestDelay] AS
>
> DECLARE @DELAYPERIOD NCHAR(12)
> SET @DELAYPERIOD = '00:00:00:001'
> DECLARE @Retries INT
> SET @Retries = 0
> DECLARE @MAXTRIES INT
> SET @MAXTRIES = 1000 -- Maximum number of tries before timing out
> DECLARE @Modified DATETIME
>
> WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN
>
> SET @Modified = GetDate() -- Set Modification Date
>
> PRINT @Retries
> PRINT @Modified
> PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR, @Modified ) ) > PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR ,
> @Modified ) )
> PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR, @Modified ) ) > PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE,
@Modified ) )
> PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND,

@Modified ) )
> PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART( MILLISECOND, > @Modified ) )
>
> SET @Retries = @Retries + 1 -- Increment loop counter and
retry >
> WAITFOR DELAY @DELAYPERIOD
>
> END
> GO
>


-------------------------------------------------------------------------- --
> -------
>
>
>



Jul 20 '05 #7

"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:Mj*****************@newsread2.news.pas.earthl ink.net...
Erland's extended proc idea is interesting but, in addition the complexity, there may be some overhead involved. Perhaps you can describe the business problem. Perhaps there are better methods besides SQL.


Btw, I want to mention the only production use I've seen of this (WAITFOR)
on our servers is I think bad programming. (i.e. it solved the symptom, but
I'm not sure it solved the underlying problem.)

Jul 20 '05 #8
Here is the scenario:

I have several hundred thousand rows of data in a MS Access database that
must be imported into SQL Server. I cannot employ a simple import since the
underlying table schema in both databases are different. So I wrote a C#
import routine to do the dirty work for me. The C# application - which
we'll call ImportData.EXE - interacts with a SQL Server 2000 Enterprise
server database through stored procs only. One of the procs -
sp_SaveImportedData - is responsible for saving the imported data. As part
of it's insert routine sp_SaveImportedData generates a timebased PrimaryKey
(see the formula below) and tries to insert the data using that PrimaryKey.
If the insertion fails because of a duplicate PK (i.e. @@ERROR = 2627)
sp_SaveImportedData is instructed to wait 1 millisecond (using WAITFOR
DELAY '00:00:00:001'), generate a new PK, and retry the insert operation.
This happens in a loop which is aborted after 1000 attempts. Any errors are
written to a text file for later reconcilliation.

If it all works, the PK should look something like 2004186773134305 which is
the result of the following concatenation

PK = dbo. PadLeft ( @Year, '0', 4 ) + dbo.PadLeft ( @DayOfYear, '0', 3 )
+ dbo. PadLeft ( @MilliSecond, '0', 3 ) + dbo.PadLeft ( @Hour, '0', 2 )
+ dbo.PadLeft ( @Minute, '0', 2 ) + dbo.PadLeft ( @Second, '0', 2 )

where PadLeft is a UDF

This allows me to insert a theoritical maximum of 1000 rows of data into the
SQL Server every second. Obviously this isn't occuring in practice. BTW
this is a non issue on an older system (PIII 667, 1GB) - everything works
flawlessly which is why the issue went un-noticed for several weeks.

The obvious solution here is to re-work how the keys are generated but I'm
attempting to better understand what are my real options.

"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:Mj*****************@newsread2.news.pas.earthl ink.net...
Erland's extended proc idea is interesting but, in addition the complexity , there may be some overhead involved. Perhaps you can describe the business problem. Perhaps there are better methods besides SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:bH*******************@read1.cgocable.net...
Guys,

I appreciate the assistance you've all provided. Any suggested work around?

"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:Mh*****************@newsread2.news.pas.earthl ink.net...
Standard Windows time API calls are only accurate to about 10 ms and this
is
apparently how SQL Server implements WAITFOR DELAY. When I run a Profiler trace, I see statements that run quickly have a reported duration of

either
0 , 13 or 16 milliseconds but nothing in between.

Below is my test script that shows the actual behavior of WAITFOR DELAY. The bottom line is that SQL Server will wait for at least the specified time
but can wait longer due to the combination of datetime rounding and timer interval resolution.

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#Metrics') IS NOT NULL DROP TABLE #Metrics
CREATE TABLE #Metrics
(
MillisecondDelay int NOT NULL,
MillisecondDelayTime datetime NOT NULL,
Retry int NOT NULL,
StartTime datetime NOT NULL,
EndTime datetime NOT NULL
)
DECLARE @MillisecondDelay int
DECLARE @MillisecondDelayTime datetime
DECLARE @Retries int
DECLARE @StartTime datetime
DECLARE @EndTime datetime

SET @MillisecondDelay = 0
WHILE @MillisecondDelay < 50
BEGIN
SET @MillisecondDelay = @MillisecondDelay + 1
SET @MillisecondDelayTime = DATEADD(ms, @MillisecondDelay,
'00:00:00:000')
SET @Retries = 0
WHILE @Retries <= 100
BEGIN
SET @Retries = @Retries + 1 -- Increment loop counter and retry SET @StartTime = getdate()
WAITFOR DELAY @MillisecondDelayTime
SET @EndTime = getdate()
INSERT INTO #Metrics VALUES(@MillisecondDelay,
@MillisecondDelayTime, @Retries, @StartTime, @EndTime)
END
END

SELECT
MillisecondDelayTime,
AVG(ActualMilliseconds) AS ActualAverageMilliseconds
FROM (
SELECT
MillisecondDelayTime,
DATEDIFF(ms, StartTime, EndTime) AS ActualMilliseconds
FROM #Metrics a
WHERE Retry > 1) AS Metrics
GROUP BY
MillisecondDelayTime
ORDER BY
MillisecondDelayTime

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Aaron W. West" <ta******@hotmail.NO.SPAM> wrote in message
news:E7********************@speakeasy.net...
> I also notice after running a few tests that WAITFOR seems to have a
> resolution of 1/100th second.
>
> Specifying a delay of '00:00:00:003' causes the same delay as if I'd
> specified 1/100th second.
>
> WAITFOR DELAY '00:00:00:013' causes a 2/100th second delay (:020 or

.02),
so
> it apparently always rounds up.
>
> The behavior seems easier to follow with less output.
>
>
> alter PROCEDURE [sp_TestDelay] AS
> DECLARE @DELAYPERIOD DATETIME
> SET @DELAYPERIOD = '00:00:00:013'
> DECLARE @Retries INT
> SET @Retries = 0
> WHILE ( @Retries <= 1000 ) BEGIN
> PRINT DATEPART( MILLISECOND, getdate() )
> SET @Retries = @Retries + 1 -- Increment loop counter and
retry > WAITFOR DELAY @DELAYPERIOD
> END
> GO
>
> exec sp_testdelay
>
> 577
> 597
> 617
> 637
> 657
> ... etc.
>
>
> "Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
> news:rS*****************@newsread2.news.pas.earthl ink.net...
> The SQL Server datetime datatype, and therefore WAITFOR DELAY, is only > accurate to 3.33 milliseconds and rounding may occur. You can see

what
is
> happening with the following
>
> DECLARE @DELAYPERIOD datetime
> SET @DELAYPERIOD = '00:00:00:001'
> SELECT @DELAYPERIOD
>
> This is documented in the SQL Server 2000 Books Online:
>
> <Excerpt href="tsqlref.chm::/ts_da-db_9xut.htm">
>
> Date and time data from January 1, 1753 through December 31, 9999, to an > accuracy of one three-hundredth of a second (equivalent to 3.33
milliseconds
> or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007
> seconds, as shown in the table.
>
> Example Rounded example
> 01/01/98 23:59:59.999 1998-01-02 00:00:00.000
>
> 01/01/98 23:59:59.995, 1998-01-01 23:59:59.997
> 01/01/98 23:59:59.996,
> 01/01/98 23:59:59.997, or
> 01/01/98 23:59:59.998
>
> 01/01/98 23:59:59.992, 1998-01-01 23:59:59.993
> 01/01/98 23:59:59.993,
> 01/01/98 23:59:59.994
>
> 01/01/98 23:59:59.990 or 1998-01-01 23:59:59.990
> 01/01/98 23:59:59.991
>
> </Excerpt>
>
> --
> Hope this helps.
>
> Dan Guzman
> SQL Server MVP
>
> "Des L. Davis" <de*******@cogeco.ca> wrote in message
> news:yq*********************@read2.cgocable.net...
> > System: Dell PowerEdge Server with 3 GB RAM, 2.4 GHz Celeron
> > Software: Microsoft SQL Server 2000 Enterprise running on Windows 2003 > > Server
> > Software: Microsoft SQL Server 2000 Enterprise running on Windows 2000 > > Server
> >
> > If you run the code below, you'll notice something odd occuring. The > > MilliSecond value does not change after a 1Millisecond delay. Is this
a
> bug
> > or am I doing something wrong?
> > Any assistance will be greatly appreciated
>


-------------------------------------------------------------------------- > --
> > -------
> > /*
> > Programmer : Des L. Davis
> > Date : July 4, 2004
> > Purpose : Testing Delayed Reaction
> > */
> >
> > CREATE PROCEDURE [sp_TestDelay] AS
> >
> > DECLARE @DELAYPERIOD NCHAR(12)
> > SET @DELAYPERIOD = '00:00:00:001'
> > DECLARE @Retries INT
> > SET @Retries = 0
> > DECLARE @MAXTRIES INT
> > SET @MAXTRIES = 1000 -- Maximum number of tries before timing
out > > DECLARE @Modified DATETIME
> >
> > WHILE ( ( @Retries <= @MAXTRIES ) ) BEGIN
> >
> > SET @Modified = GetDate() -- Set Modification Date
> >
> > PRINT @Retries
> > PRINT @Modified
> > PRINT 'Year = ' + CONVERT( NCHAR(4), DATEPART( YEAR,

@Modified ) )
> > PRINT 'DayOfYear = ' + CONVERT( NCHAR(3), DATEPART( DAYOFYEAR , > > @Modified ) )
> > PRINT 'Hour = ' + CONVERT( NCHAR(2), DATEPART( HOUR,

@Modified ) )
> > PRINT 'Minute = ' + CONVERT( NCHAR(2), DATEPART( MINUTE,
> @Modified ) )
> > PRINT 'Second = ' + CONVERT( NCHAR(2), DATEPART( SECOND,
@Modified ) )
> > PRINT 'MilliSecond = ' + CONVERT( NCHAR(3), DATEPART(

MILLISECOND,
> > @Modified ) )
> >
> > SET @Retries = @Retries + 1 -- Increment loop counter and

retry > >
> > WAITFOR DELAY @DELAYPERIOD
> >
> > END
> > GO
> >
>


--------------------------------------------------------------------------
> --
> > -------
> >
> >
> >
>
>
>



Jul 20 '05 #9
Des L. Davis (de*******@cogeco.ca) writes:
I have several hundred thousand rows of data in a MS Access database
that must be imported into SQL Server. I cannot employ a simple import
since the underlying table schema in both databases are different. So I
wrote a C# import routine to do the dirty work for me. The C#
application - which we'll call ImportData.EXE - interacts with a SQL
Server 2000 Enterprise server database through stored procs only. One
of the procs - sp_SaveImportedData - is responsible for saving the
imported data.
Standard comment: the sp_ prefix is reserved for system procedures and SQL
Server will first look for these in master.
If it all works, the PK should look something like 2004186773134305
which is the result of the following concatenation

PK = dbo. PadLeft ( @Year, '0', 4 ) + dbo.PadLeft ( @DayOfYear, '0', 3 )
+ dbo. PadLeft ( @MilliSecond, '0', 3 ) + dbo.PadLeft ( @Hour, '0', 2 )
+ dbo.PadLeft ( @Minute, '0', 2 ) + dbo.PadLeft ( @Second, '0', 2 )

where PadLeft is a UDF

This allows me to insert a theoritical maximum of 1000 rows of data into
the SQL Server every second.
As you have learnt now, the theoretical maximum is 333 rows, and the actual
number probably even lower.
The obvious solution here is to re-work how the keys are generated but I'm
attempting to better understand what are my real options.


It wasn't clear to me why have chosen this scheme and not a plain IDENTITY
column.

If you for some reason want the keys to be scattered, you could use a
uniqueidentifier column and assigned a value with newid().
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #10
> The obvious solution here is to re-work how the keys are generated but I'm
attempting to better understand what are my real options.


IMHO, you'll be better off generating a surrogate values using an different
algorithm. The IDENTITY approach Erland suggested will perform well,
guarantee uniqueness and handle concurrency. I'm interested in knowing why
you are deliberately assigning non-sequential values. More often than not,
'hot spots' are a good thing in SQL 7 and above due to row-level locking.

From your description, it appears there is a cursor loop involved somewhere,
although it's unclear to me whether this is in sp_SaveImportedData, another
proc or ImportData.EXE. In any case, if you want to keep your existing
algorithm, consider replacing the millisecond component with your own
sequence number that is initialized whenever the time component changes.
The untested Transact-SQL script below illustrates this technique.

DECLARE @DateComponent char(7)
DECLARE @TimeComponent char(6)
DECLARE @LastDateComponent char(7)
DECLARE @LastTimeComponent char(6)
DECLARE @PK char(16)
DECLARE @Sequence int

WHILE --some loop condition
BEGIN

SET @DateComponent =
dbo. PadLeft ( @Year, '0', 4 ) +
dbo.PadLeft ( @DayOfYear, '0', 3 )
SET @TimeComponent =
dbo.PadLeft ( @Hour, '0', 2 ) +
dbo.PadLeft ( @Minute, '0', 2 ) +
dbo.PadLeft ( @Second, '0', 2 )

IF @DateComponent + @TimeComponent <> @LastDateComponent +
@LastTimeComponent
BEGIN
SET @Sequence = 0
SET @LastDateComponent = @DateComponent
SET @LastTimeComponent = @TimeComponent
END
ELSE
BEGIN
SET @Sequence = @Sequence + 1
END
SET @PK =
@DateComponent +
dbo.PadLeft ( @Sequence, '0', 3 ) +
@TimeComponent

--other code here
END

--
Hope this helps.

Dan Guzman
SQL Server MVP
Jul 20 '05 #11
I'm in the process of re-writing the key generating algorithm to eliminate
the WAITFOR DELAY. Thanks for all the valuable input.

Dan,

The keys appear non-sequential if you consider them numerical values.
However, they are sequential if you consider them time-based entities.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:aM****************@newsread1.news.pas.earthli nk.net...
The obvious solution here is to re-work how the keys are generated but I'm attempting to better understand what are my real options.
IMHO, you'll be better off generating a surrogate values using an

different algorithm. The IDENTITY approach Erland suggested will perform well,
guarantee uniqueness and handle concurrency. I'm interested in knowing why you are deliberately assigning non-sequential values. More often than not, 'hot spots' are a good thing in SQL 7 and above due to row-level locking.

From your description, it appears there is a cursor loop involved somewhere, although it's unclear to me whether this is in sp_SaveImportedData, another proc or ImportData.EXE. In any case, if you want to keep your existing
algorithm, consider replacing the millisecond component with your own
sequence number that is initialized whenever the time component changes.
The untested Transact-SQL script below illustrates this technique.

DECLARE @DateComponent char(7)
DECLARE @TimeComponent char(6)
DECLARE @LastDateComponent char(7)
DECLARE @LastTimeComponent char(6)
DECLARE @PK char(16)
DECLARE @Sequence int

WHILE --some loop condition
BEGIN

SET @DateComponent =
dbo. PadLeft ( @Year, '0', 4 ) +
dbo.PadLeft ( @DayOfYear, '0', 3 )
SET @TimeComponent =
dbo.PadLeft ( @Hour, '0', 2 ) +
dbo.PadLeft ( @Minute, '0', 2 ) +
dbo.PadLeft ( @Second, '0', 2 )

IF @DateComponent + @TimeComponent <> @LastDateComponent +
@LastTimeComponent
BEGIN
SET @Sequence = 0
SET @LastDateComponent = @DateComponent
SET @LastTimeComponent = @TimeComponent
END
ELSE
BEGIN
SET @Sequence = @Sequence + 1
END
SET @PK =
@DateComponent +
dbo.PadLeft ( @Sequence, '0', 3 ) +
@TimeComponent

--other code here
END

--
Hope this helps.

Dan Guzman
SQL Server MVP

Jul 20 '05 #12
> The keys appear non-sequential if you consider them numerical values.
However, they are sequential if you consider them time-based entities.
The code snippet in your earlier post is concatenating the millisecond
component before the remainder of time. Consequently, sequential time
values won't be stored consecutively in the ordered index. This is
consideration because it introduces index fragmentation and lessens the
effectiveness of index.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:D3*******************@read1.cgocable.net... I'm in the process of re-writing the key generating algorithm to eliminate
the WAITFOR DELAY. Thanks for all the valuable input.

Dan,

The keys appear non-sequential if you consider them numerical values.
However, they are sequential if you consider them time-based entities.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:aM****************@newsread1.news.pas.earthli nk.net...
The obvious solution here is to re-work how the keys are generated but I'm attempting to better understand what are my real options.


IMHO, you'll be better off generating a surrogate values using an

different
algorithm. The IDENTITY approach Erland suggested will perform well,
guarantee uniqueness and handle concurrency. I'm interested in knowing

why
you are deliberately assigning non-sequential values. More often than

not,
'hot spots' are a good thing in SQL 7 and above due to row-level locking.
From your description, it appears there is a cursor loop involved

somewhere,
although it's unclear to me whether this is in sp_SaveImportedData,

another
proc or ImportData.EXE. In any case, if you want to keep your existing
algorithm, consider replacing the millisecond component with your own
sequence number that is initialized whenever the time component changes.
The untested Transact-SQL script below illustrates this technique.

DECLARE @DateComponent char(7)
DECLARE @TimeComponent char(6)
DECLARE @LastDateComponent char(7)
DECLARE @LastTimeComponent char(6)
DECLARE @PK char(16)
DECLARE @Sequence int

WHILE --some loop condition
BEGIN

SET @DateComponent =
dbo. PadLeft ( @Year, '0', 4 ) +
dbo.PadLeft ( @DayOfYear, '0', 3 )
SET @TimeComponent =
dbo.PadLeft ( @Hour, '0', 2 ) +
dbo.PadLeft ( @Minute, '0', 2 ) +
dbo.PadLeft ( @Second, '0', 2 )

IF @DateComponent + @TimeComponent <> @LastDateComponent +
@LastTimeComponent
BEGIN
SET @Sequence = 0
SET @LastDateComponent = @DateComponent
SET @LastTimeComponent = @TimeComponent
END
ELSE
BEGIN
SET @Sequence = @Sequence + 1
END
SET @PK =
@DateComponent +
dbo.PadLeft ( @Sequence, '0', 3 ) +
@TimeComponent

--other code here
END

--
Hope this helps.

Dan Guzman
SQL Server MVP


Jul 20 '05 #13
Dan,

You've been awesome and I'm very appreciative. Thank you
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:b2*****************@newsread2.news.pas.earthl ink.net...
The keys appear non-sequential if you consider them numerical values.
However, they are sequential if you consider them time-based entities.


The code snippet in your earlier post is concatenating the millisecond
component before the remainder of time. Consequently, sequential time
values won't be stored consecutively in the ordered index. This is
consideration because it introduces index fragmentation and lessens the
effectiveness of index.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Des L. Davis" <de*******@cogeco.ca> wrote in message
news:D3*******************@read1.cgocable.net...
I'm in the process of re-writing the key generating algorithm to eliminate
the WAITFOR DELAY. Thanks for all the valuable input.

Dan,

The keys appear non-sequential if you consider them numerical values.
However, they are sequential if you consider them time-based entities.
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:aM****************@newsread1.news.pas.earthli nk.net...
> The obvious solution here is to re-work how the keys are generated but
I'm
> attempting to better understand what are my real options.

IMHO, you'll be better off generating a surrogate values using an

different
algorithm. The IDENTITY approach Erland suggested will perform well,
guarantee uniqueness and handle concurrency. I'm interested in
knowing why
you are deliberately assigning non-sequential values. More often than

not,
'hot spots' are a good thing in SQL 7 and above due to row-level

locking.
From your description, it appears there is a cursor loop involved

somewhere,
although it's unclear to me whether this is in sp_SaveImportedData,

another
proc or ImportData.EXE. In any case, if you want to keep your

existing algorithm, consider replacing the millisecond component with your own
sequence number that is initialized whenever the time component changes. The untested Transact-SQL script below illustrates this technique.

DECLARE @DateComponent char(7)
DECLARE @TimeComponent char(6)
DECLARE @LastDateComponent char(7)
DECLARE @LastTimeComponent char(6)
DECLARE @PK char(16)
DECLARE @Sequence int

WHILE --some loop condition
BEGIN

SET @DateComponent =
dbo. PadLeft ( @Year, '0', 4 ) +
dbo.PadLeft ( @DayOfYear, '0', 3 )
SET @TimeComponent =
dbo.PadLeft ( @Hour, '0', 2 ) +
dbo.PadLeft ( @Minute, '0', 2 ) +
dbo.PadLeft ( @Second, '0', 2 )

IF @DateComponent + @TimeComponent <> @LastDateComponent +
@LastTimeComponent
BEGIN
SET @Sequence = 0
SET @LastDateComponent = @DateComponent
SET @LastTimeComponent = @TimeComponent
END
ELSE
BEGIN
SET @Sequence = @Sequence + 1
END
SET @PK =
@DateComponent +
dbo.PadLeft ( @Sequence, '0', 3 ) +
@TimeComponent

--other code here
END

--
Hope this helps.

Dan Guzman
SQL Server MVP



Jul 20 '05 #14
Glad it helped.

--
Dan Guzman
SQL Server MVP
Jul 20 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
22617
by: NickName | last post by:
Env: SQL Server 2000 It actually waited for 50 seconds instead of 5, tried WAITFOR DELAY '000:00:005' and WAITFOR DELAY '000:00:5' respectively, and got same behavior. Bug or ? TIA
6
5532
by: lucifer | last post by:
hi i need to insert delay in my program what function should i use the old delay is not supported by the VC6
20
2990
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
11
21230
by: Maheshkumar.R | last post by:
Hi groups, How i can introduce some milliseconds delay in application. How i can do achieve this... let me clearly say... (1) I'm displaying slices of medical images. For framerate - for...
7
2930
by: mfeingold | last post by:
I am working on a system, which among other things includes a server and a ..net control sitting in an html page and connected to the server. I ran into a couple of problems, you guys might have...
7
3299
by: MLH | last post by:
This was contributed by someone in this NG last year... Sub WaitFor(psngSeconds As Single) ' wait for specified number of seconds ' Copyright Trevor Best (tre...@besty.org.uk) <-OK, so I added...
17
11643
dmjpro
by: dmjpro | last post by:
Have a look at my code snippet. Process l_p = Runtime.getRuntime().exec("my command"); l_p.waitFor(); //here the program hangs out Then i go for a site and found it's solution .. Simply...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.