SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
lukavog
lukavog
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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 Smile 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? Cool

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
r.dragoi-1095738
r.dragoi-1095738
SSC-Addicted
SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)SSC-Addicted (422 reputation)

Group: General Forum Members
Points: 422 Visits: 2475
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);
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25960 Visits: 17520
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.

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)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16400 Visits: 19077
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25960 Visits: 17520
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. :-P 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.

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)
Keith Tate
Keith Tate
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1020 Visits: 979
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search