Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 issue when passing variable to query

Author  Topic 

jmhc101
Starting Member

3 Posts

Posted - 2013-01-18 : 08:34:01
hi,

this query works:

select ap.*
from date_lookup d
outer apply
dbo.udf_ActivePlacementNumbersByDay(d.date_full) ap
where d.date_full >= '20130101'
and d.date_full <= '20130103'

yet this one doesn't (it hangs):

declare @startDate date
declare @endDate date

set @startDate = '20130101'
set @endDate = '20130103'

select ap.*
from date_lookup d
outer apply
dbo.udf_ActivePlacementNumbersByDay(d.date_full) ap
where d.date_full >= @startDate
and d.date_full <= @endDate

I'm assuming this must be a data type issue for the date variables. Can someone assist me in how I can pass a variable into this query and what data types to use correctly.

If you need more context about the query - please let me know.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-18 : 13:20:11
It could be because of a bad execution plan, or it could be because of the data types. Whether it is due to data types is easy to check change your WHERE clause to:
where d.date_full >= CAST(@startDate as DATE)
and CAST(d.date_full <= @endDate as DATE)
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-01-18 : 15:03:38
Is this used in a stored procedure? If so, it's possible you might be a victom of parameter sniffing.
http://blogs.technet.com/b/mdegre/archive/2012/03/19/what-is-parameter-sniffing.aspx
Go to Top of Page
   

- Advertisement -