Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL works fine, but doe's not work called by a Stored Procedure Expand / Collapse
Author
Message
Posted Friday, November 22, 2013 2:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, November 22, 2013 8:39 AM
Points: 7, Visits: 18
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

Post #1516687
Posted Friday, November 22, 2013 4:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 3:36 AM
Points: 25, Visits: 1,264
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);
Post #1516715
Posted Friday, November 22, 2013 7:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1516787
Posted Friday, November 22, 2013 8:20 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 11:05 AM
Points: 3,513, Visits: 7,567
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1516807
Posted Friday, November 22, 2013 8:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1516827
Posted Friday, November 22, 2013 9:23 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: 2 days ago @ 11:38 AM
Points: 588, Visits: 895
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
Post #1516854
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse