By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

WAITFOR DELAY

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
Glad it helped.

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

This discussion thread is closed

Replies have been disabled for this discussion.