SQL works fine, but doe's not work called by a Stored Procedure

  • Hi, I have this code working fine:

    DECLARE @startdate datetime = convert(datetime, '2013-05-24 20:00:00', 120);

    DECLARE @step bigint = 1;

    DECLARE @nextStartdate datetime = DATEADD(ss,@step*3600,@startdate);

    SELECT a.c1+ b.c2 + c.c3 + d.c4

    FROM

    (

    SELECT COALESCE(DATEDIFF(second, start_delay, end_delay),0) as c1

    FROM DLY_DELAY

    WHERE start_delay >= @startdate

    AND end_delay <= @nextStartdate

    ) a,

    (SELECT COALESCE(DATEDIFF(second, start_delay, @nextStartdate ),0) as c2

    FROM DLY_DELAY

    WHERE start_delay >= @startdate

    AND start_delay < @nextStartdate

    AND end_delay > @nextStartdate

    ) b,

    (

    SELECT COALESCE(DATEDIFF(second, @startdate, end_delay ),0) as c3

    FROM DLY_DELAY

    WHERE start_delay < @startdate

    AND end_delay <= @nextStartdate

    AND end_delay > @startdate

    ) c,

    (

    SELECT COALESCE((@step *3600),0) as c4

    FROM DLY_DELAY

    WHERE start_delay < @startdate

    AND end_delay > @nextStartdate

    ) d

    Returning exactly what I need 🙂 An integer number, representing the duration...

    but... integrating the same code in a Stored Procedure like this:

    ALTER PROCEDURE GetDelayIntervalDuration(@startdate datetime, @step bigint)

    AS

    BEGIN

    DECLARE @TempTable TABLE (result bigint)

    DECLARE @nextStartdate datetime = DATEADD(ss,@step*3600,@startdate);

    INSERT INTO @TempTable(result)

    SELECT a.c1+ b.c2 + c.c3 + d.c4

    FROM

    (

    SELECT COALESCE(DATEDIFF(second, start_delay, end_delay),0) as c1

    FROM DLY_DELAY

    WHERE start_delay >= @startdate

    AND end_delay <= @nextStartdate

    ) a,

    (SELECT COALESCE(DATEDIFF(second, start_delay, @nextStartdate ),0) as c2

    FROM DLY_DELAY

    WHERE start_delay >= @startdate

    AND start_delay < @nextStartdate

    AND end_delay > @nextStartdate

    ) b,

    (

    SELECT COALESCE(DATEDIFF(second, @startdate, end_delay ),0) as c3

    FROM DLY_DELAY

    WHERE start_delay < @startdate

    AND end_delay <= @nextStartdate

    AND end_delay > @startdate

    ) c,

    (

    SELECT COALESCE((@step *3600),0) as c4

    FROM DLY_DELAY

    WHERE start_delay < @startdate

    AND end_delay > @nextStartdate

    ) d

    -- output

    SELECT result FROM @TempTable

    END;

    with the same input parameters returns an empty table/null/no results.

    DECLARE @startdate datetime = convert(datetime, '2013-05-24 00:00:00', 120);

    DECLARE @step bigint = 1;

    execute dbo.GetDelayIntervalDuration @startdate, @step;

    What I'm I missing? 😎

    SQL for my table

    CREATE TABLE [dbo].[DLY_DELAY] (

    [DELAY_CNT] [dbo].[LongInt] NOT NULL,

    [START_DELAY] [dbo].[DTimeNull] NULL,

    [END_DELAY] [dbo].[DTimeNull] NULL)

    SQL - some data:

    insert into [dbo].[DLY_DELAY]([DELAY_CNT],[START_DELAY],[END_DELAY]) values (36,'2013-05-24 20:47:00','2013-05-24 20:50:00')

    insert into [dbo].[DLY_DELAY]([DELAY_CNT],[START_DELAY],[END_DELAY]) values (37,'2013-05-24 20:57:00','2013-05-24 21:03:00')

    insert into [dbo].[DLY_DELAY]([DELAY_CNT],[START_DELAY],[END_DELAY]) values (38,'2013-05-24 19:57:00','2013-05-24 20:02:00')

    insert into [dbo].[DLY_DELAY]([DELAY_CNT],[START_DELAY],[END_DELAY]) values (39,'2013-05-24 19:57:00','2013-05-24 21:03:00')

    GO

    Thanks Luka :w00t:

  • Are you sure that the same parameters are used !?

    -- DECLARE @startdate datetime = convert(datetime, '2013-05-24 00:00:00', 120);

    DECLARE @startdate datetime = convert(datetime, '2013-05-24 20:00:00', 120);

  • This is a sql server site and it appears the ddl you posted is for Oracle. Keep in mind that any code you receive the works well in sql server may or may not work well in Oracle. I would recommend you find an Oracle site. The help you receive there will be better suited to your environment.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Why are you inserting into a table variable and then selecting from it instead of doing the select directly as you do with your original query?

    Sean, I can't see why you say that it's Oracle. The queries look like SQL Server and I suppose Luka is using user defined data types.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (11/22/2013)


    Why are you inserting into a table variable and then selecting from it instead of doing the select directly as you do with your original query?

    Sean, I can't see why you say that it's Oracle. The queries look like SQL Server and I suppose Luka is using user defined data types.

    Didn't think about user defined datatypes. 😛 I was helping the same OP yesterday and it was a mix of Oracle and Sql.

    I agree with your question of why insert into a temp table to just turn around and pull it back out.

    Also, the date variable population can be made a lot simpler.

    DECLARE @startdate datetime = '2013-05-24 20:00:00'

    There is no need to format a string to that is the same format as the string itself which can be cast to a datetime as is.

    All that being said, it looks like there should be no issue with the code. But we can't actually load this to test on our systems because of the datatypes.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • In you query you are starting with 5/24/2013 20:00:00, but when you call the stored proc you are using 00:00:00. Change the call to the sproc to use 20:00:00 and it works



    Microsoft Certified Master - SQL Server 2008
    Follow me on twitter: @keith_tate

    Forum Etiquette: How to post data/code on a forum to get the best help[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

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