473,789 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

@createddatelow er datetime

WHERE dbo.tblCaseHist ory.eventdate > dateadd(d,-7,@createddatel ower )
AND dbo.tblCaseHist ory.eventdate < dateadd(d,-6,@createddatel ower ) (45
seconds)

vs.

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

thanks for your help,
Paul

Nov 23 '05 #1
5 2870
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 (@createddatelo wer datetime) AS
...
WHERE eventdate > dateadd(day,-7,@createddatel ower)
...

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?

@createddatelow er datetime

WHERE dbo.tblCaseHist ory.eventdate > dateadd(d,-7,@createddatel ower )
AND dbo.tblCaseHist ory.eventdate < dateadd(d,-6,@createddatel ower ) (45
seconds)

vs.

WHERE dbo.tblCaseHist ory.eventdate > dateadd(d,-7,'11/15/05') AND
dbo.tblCaseHist ory.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
1732
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
2060
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 executed, sMyVariable will have the new value. My solution (if this doesn't exist) would've been to add the value to the resultset (make it part of the Select statement) Thanks.
1
2113
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 = .Parameters.Add("@EstConstructionCost_10", System.Data.SqlDbType.Decimal, 9).Value = CDbl(txtEstConstructionCost.Text) ' Dim p1 As SqlParameter = .Parameters.Add("@EstConstructionCost_10", System.Data.SqlDbType.Decimal).Value = 10
1
1239
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
2328
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. System.NullReferenceException: Object reference not set to an instance of an object. I thought it is because there is a statement cmd.Parameters.Clear() in
5
1635
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 string, list_employee 4 instead of splitting into list_employee
1
1376
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 logic in the stored procedure so it returns a value in certain cases. How to detect the return value from that stored procedure that is used by the DetailsView control. Many Thanks,
2
1115
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
3323
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
9665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10199
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.