473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Optimizing Stored Procedure with Datetime parameter

Hi,

When I pass a date time parameter the stored procedure takes about 45
seconds, when I hard code the parameter it returns in 1 second. How can
I rewrite my stored procedure?

@createddatelower datetime

WHERE dbo.tblCaseHistory.eventdate > dateadd(d,-7,@createddatelower )
AND dbo.tblCaseHistory.eventdate < dateadd(d,-6,@createddatelower ) (45
seconds)

vs.

WHERE dbo.tblCaseHistory.eventdate > dateadd(d,-7,'11/15/05') AND
dbo.tblCaseHistory.eventdate < dateadd(d,-6,'11/15/05') (1 second)

thanks for your help,
Paul

Nov 23 '05 #1
5 2848
Do you get the same (correct) result with both queries?

Nov 23 '05 #2
Yes, the result is the same

Nov 23 '05 #3
I've read that perhaps the sp is not using the index. The table i'm
searching is a joined table in the sp...Do you know how i can force it
to use the index? Then name of the index is IndexDate

thanks,
Paul

Nov 23 '05 #4
>Do you know how i can force it to use the index?

Check out "forceplan" as in:

set forceplan on

Nov 23 '05 #5
Paul,

Your stored procedure could benefit from parameter sniffing if you
change the local variable into a parameter, as in:

CREATE PROCEDURE MyProcedure (@createddatelower datetime) AS
...
WHERE eventdate > dateadd(day,-7,@createddatelower)
...

EXEC MyProcedure '20051115'

HTH,
Gert-Jan

paulmac106 wrote:

Hi,

When I pass a date time parameter the stored procedure takes about 45
seconds, when I hard code the parameter it returns in 1 second. How can
I rewrite my stored procedure?

@createddatelower datetime

WHERE dbo.tblCaseHistory.eventdate > dateadd(d,-7,@createddatelower )
AND dbo.tblCaseHistory.eventdate < dateadd(d,-6,@createddatelower ) (45
seconds)

vs.

WHERE dbo.tblCaseHistory.eventdate > dateadd(d,-7,'11/15/05') AND
dbo.tblCaseHistory.eventdate < dateadd(d,-6,'11/15/05') (1 second)

thanks for your help,
Paul

Nov 23 '05 #6

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

Similar topics

1
by: crisp99 | last post by:
Hi, I have a SQL Stored Procedure : CREATE PROCEDURE spFilterOne @city varchar(25) AS SELECT * FROM tblCities WHERE tblCities.strCity = @city ORDER BY tblCities.strName
7
by: VMI | last post by:
Can I call a stored proc from .net with a parm that changes during the SP? For example, I call the SP with parameter sMyVariable (which changes in the SP), and when I access it after the SP is...
1
by: Mona | last post by:
I have a stored procedure with a decimal parameter . how can send the decimal value? I try this but it gives an error , invalid cast specified. Dim p1 As SqlParameter =...
1
by: Craig Buchanan | last post by:
Has any one been able to get MS DataAccess Block code to populate a return parameter in the ExecuteReader method? If so, how? Thanks, Craig Buchanan
0
by: rockdale | last post by:
Hi, All How to get the output parameter's value when you use the SQLHelper (Microsoft Data Access Block)? When I try to access my ourput parm I got the following error. ...
5
by: Alan T | last post by:
I have a string passed from another function, eg list_employee 4 This will call the stored procedure list_employee to get details of employee of id 4. Is there a way to just use this...
1
by: =?Utf-8?B?QmlzaG95?= | last post by:
Hi all, I have an asp.net 2.0 web site with a DetailsView control to select and insert data from database. I made the insert statement for that DetailsView by a stored procedure. I made some...
2
by: simonakiki | last post by:
hi how can if call a strored prosedure from sql server with parameters my procedure is ALTER PROCEDURE . ( @p_version nvarchar(100),
3
by: DhruviTrivedi | last post by:
Is there any way to get parameter value from variable like @Param1 varchar(50) @Param2 varchar(50) set Param1 ='value1' while --condition-- begin
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...

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.