Declared datetime Parameter not working correctly in sql 2014

  • We have a Data base we migrated to 2014 from 2008, everything is working fine except we have a stored proc that has the following

    it just hangs, but if I replace the parameter in the select statement with getdate() it works. or if i change the compatibility mode to 2008.

    when I am debug and hover over the @today I can see it is properly set to todays date. very very strange

    Declare @today datetime
    Set @today = getdate()

    select * from table where insertdate < @today
  • This might have to do with what SQL Server would expect as a Default value, GetDate()'s Default Value is non existent so precompiling query plans (which is what sp's are supposed to do in order to work faster than plain, dynamic queries.

     

    You could try some sort of approach like this instead:

     

    ALTER PROC DisplayDate 
    (
    @DateVar DATETIME = NULL
    ) AS
    BEGIN
    set @DateVar=ISNULL(@DateVar,GETDATE())

    --the SP stuff here
    SELECT @DateVar
    END
    GO
  • --Deleted

    • This reply was modified 4 years, 10 months ago by  Phil Parkin.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • my question is why does it work with compatibility mode 2008 not with comparability mode 2014?

    because I have 100's of procs that use the declare @today datetime

  • Lee Hopkins wrote:

    my question is why does it work with compatibility mode 2008 not with comparability mode 2014? because I have 100's of procs that use the declare @today datetime

     

    Can you provide a self-contained example which demonstrates this, so that we can verify it too?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • I'm guessing this has to do with the new  Cardinality Estimator that was introduced with SQL Server 2014.

    We've seen some strange goings on with this feature if the statistics aren't quite up-to-date. So I'd try to first instruct SQL Server to use the old Cardinality Estimator by adding OPTION(QUERYTRACEON 9481) to the query.

    If the query runs as expected with this trace flag set, you know it's the Cardinality Estimator that makes SQL Server choose a bad execution plan (like doing a table scan instead of doing an index seek).

    You might also try to update the statistics on the table and see if that changes anything.

  • good suggestions i will take a look.

     

  • Thanks for the help using OPTION(QUERYTRACEON 9481) , corrected the issue I also tried option (recompile) and it worked also. so the  Cardinality Estimator is a new feature to 2014, to help pick a better excursion plan?

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply