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 2000 Forums
 Transact-SQL (2000)
 Strange issue with linked table inside SP

Author  Topic 

jalba07
Starting Member

4 Posts

Posted - 2006-11-14 : 14:28:10
I'm guessing this is a linked table issue since there is nothing else out of the ordinary. Running SQL 2000 with all service packs.

The select statement below runs fine, returns data in 2 seconds...

/#temp and declarations done/
========================================
SELECT SUM(I.[SAMPLE]) as SAMPLE_SUM, 
I.SAMPLE_DATE,
T.TOPIC_DESCRIPTION
FROM ENS_MV.eNERGYSolveID.dbo.INTERVAL AS I
JOIN TOPICS AS T ON (I.TOPIC_ID = T.TOPIC_ID)
JOIN SUBMETER AS SM ON (I.DEVICE_ID=SM.DEVICE_ID)
JOIN #TEMP AS TE ON SM.SUB_METER_ID = TE.SUBMETER_ID
WHERE I.TOPIC_ID = @int_reading_type
AND I.SAMPLE_DATE BETWEEN @dt_date_from AND @dt_date_to
GROUP BY I.SAMPLE_DATE, T.TOPIC_DESCRIPTION
ORDER BY I.SAMPLE_DATE
=============================================


However when I pop that into a stored procedure, the query doesn't return (I have let it run for upwards of 20 minutes)...below are the contents of the SP

=============================================
CREATE PROC TEST (
@vch_submeter_id VARCHAR(1000),
@dt_date_from DATETIME,
@dt_date_to DATETIME,
@int_reading_type INT=NULL,
@int_interval INT=NULL
)
AS
SET NOCOUNT ON

DECLARE @comma_place INT
DECLARE @int_submeter_id VARCHAR(50)

CREATE TABLE #TEMP(SUBMETER_ID VARCHAR(50))

WHILE len(@vch_submeter_id) > 0
BEGIN
SET @comma_place=patindex('%;%',@vch_submeter_id)
SET @int_submeter_id = substring(@vch_submeter_id, 1, @comma_place-1)
SET @vch_submeter_id = substring(@vch_submeter_id, @comma_place+1, len(@vch_submeter_id))
INSERT INTO #TEMP(SUBMETER_ID)
VALUES(@int_submeter_id)
END

IF @int_interval = 15
BEGIN

SELECT SUM(I.[SAMPLE]) as SAMPLE_SUM, I.SAMPLE_DATE, T.TOPIC_DESCRIPTION
FROM ENS_MV.eNERGYSolveID.dbo.INTERVAL AS I
JOIN TOPICS AS T ON (I.TOPIC_ID = T.TOPIC_ID)
JOIN SUBMETER AS SM ON (I.DEVICE_ID=SM.DEVICE_ID)
JOIN #TEMP AS TE ON SM.SUB_METER_ID = TE.SUBMETER_ID
WHERE I.TOPIC_ID = @int_reading_type
AND I.SAMPLE_DATE BETWEEN @dt_date_from AND @dt_date_to
GROUP BY I.SAMPLE_DATE, T.TOPIC_DESCRIPTION
ORDER BY I.SAMPLE_DATE
END
=================================================


Am I doing something wrong?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-14 : 14:33:49
Start to debug how many records are inserted in #TEMP table.
Comment out IF case. Replace with select * from #temp

Check to see that the expected number of records are in the #temp table.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jalba07
Starting Member

4 Posts

Posted - 2006-11-14 : 14:37:24
Thanks for the quick reply. There are 3 records in the temp table, which is what I expect.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-14 : 14:42:14
No empty rows or rows with NULL?
Which values do @int_reading_type, @dt_date_from and @dt_date_to have?


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jalba07
Starting Member

4 Posts

Posted - 2006-11-14 : 14:46:24
Nope, the rows are 3 ids (912,913,914), nothing else.

The sp declaration is EXEC TEST '912;913;914;', '1/13/2006', '1/14/2006', 1621, 15

I am 100% certain there is data for those values, since it works outside of the SP as a regular select statement.

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-11-14 : 14:50:36
Try to comment out every JOINed table. Start only with the FROM table.
When that works, add ONE new joined table. Run again.
Then you see where things go wrong.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

jalba07
Starting Member

4 Posts

Posted - 2006-11-14 : 15:25:11
That excercise got me to the problem. The interval table is about 20+million rows, so I needed to tie the join of the temp table directly to the Interval table. Thanks a bunch for taking the time to help!
Go to Top of Page

kapinak
Starting Member

4 Posts

Posted - 2007-12-11 : 16:10:36
quote:
Originally posted by jalba07

That excercise got me to the problem. The interval table is about 20+million rows, so I needed to tie the join of the temp table directly to the Interval table. Thanks a bunch for taking the time to help!




did u ever resolve this? i am having simiar issues.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-11 : 16:20:54
Reorganize the FROM and JOIN and start with the table holding least records.
Add a hint (FORCE ORDER) at the very end of query.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-11 : 16:22:46
Read this topic for more learning
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=91539



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -