Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Slow SP with date parameters solved.

By Virgilio Licovali Bustos Ramirez,

When i use datetime fields in the where clause like timestampfield > {ts '2010-01-01 00:00;00'} and timestampfield < {ts '2010-01-31 23:59:59.998'} or timestampfield between {ts '2010-01-01 00:00:00'} and {ts '2010-01-31 23:59:59'} the query runs fine, but if i need to use it on stored procedeures like parameters like this:

CREATE PROCEDURE withDates

@d1 as datetime,

@d2 as datetime

AS

BEGIN

select * from tablewithdates where datetimefield between @d1 and @d2

END

 

The Procedure becomes slow, if try the same query with the direct values the query runs very fast. I don't know if there is an answer to this or if it is a bug, but, in many forums the people has the same problem: very slow stored procedures that uses date parameters.

If you use the code that i propose. The SP becomes fast as if you put the direct values. Enjoy.

Total article views: 2816 | Views in the last 30 days: 1
 
Related Articles
FORUM

datetime query issue

datetime query issue

FORUM

DateTime Problem

Datetime

FORUM

datetime based query

help mewritingthis datetime based query

FORUM

Stored procedure slower then query

Stored procedure slower then query

FORUM
 
Contribute