Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2005, 02:23 AM
Authorized User
 
Join Date: Aug 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Temporary tables

I am attempting to sort some data using temporary tables in SQL Server 2000. The data is a set of time-stamped pressure meter rate-of-change readings (Delta), for which I want to find the statistically significant drops between particular time limits.

Specifically my queries are:

1. Sort for the rows which fall between the time limits, and for the meters I am interested in, and select into #PDropTemp:

SELECT [TagIndex],[Timestamp],[Delta] AS [Drop]
INTO #PDropTemp
FROM [<original table>]
WHERE ([Timestamp] > @MinTime)
AND ([Timestamp] <@MaxTime)
AND (ValQuality = 192)
AND ((TagIndex = 19) OR (TagIndex = 59))

2. Sort for those drops which are greater (ie more negative) than 3 times the standard deviation for that particular meter, and also where the average for the meter is not zero. Selected into #PDropFiltered

SELECT T1.TagIndex,T1.[Timestamp],T1.[Drop]
INTO #PDropFiltered
FROM #PDropTemp_DM_AggregatedPressure T1
WHERE (T1.[Drop] < -3 *
    (
    SELECT STDEV(T2.[Drop])
    FROM #PDropTemp_DM_AggregatedPressure T2
    WHERE (T2.TagIndex = T1.TagIndex)
    ))
AND (
    (
    SELECT AVG(T3.[Drop])
    FROM #PDropTemp_DM_AggregatedPressure T3
    WHERE (T3.TagIndex = T1.TagIndex)
    ) <> 0)
ORDER BY T1.[Drop]

3. After doing this for potentially a number of different source tables I finally get everything from #PDropFiltered:

SELECT * FROM #PDropFiltered

[I could probably omit the last query, and select the middle query directly into memory, but nevermind...]

Executing these queries in code throws an exception telling me that #PDropTemp is an invalid object name. What is the problem? Is it the fact that I am doing a nested query?

Some other relevant points are:

1. I keep the same connection open throughout, so I assumed that the temporary tables would not be thrown away between queries.
2. These queries work fine in query analyzer.
3. If I replace these queries with simple select queries from Northwind it works fine in code.
4. If I use public temporary tables, ie ##PDropTemp it works fine. There shouldn't be an issue for concurrency because there is only one 'user' at a time, but I'm a little unclear about when public temporary tables get dropped - they are still there in tempdb after I close the connection.

Can anyone help?

 
Old July 22nd, 2005, 02:26 AM
Authorized User
 
Join Date: Aug 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops - forget to do some editing - wherever you see #PDropTemp_DM_AggregatedPressure it should actually read #PDropTemp. I thought it would make it easier to read...

 
Old August 3rd, 2005, 08:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

eadred,

I don't have the solution to your problem, but I can tell you what's causing it (I've been trying to solve this myself for a few hours!). It is the use of the temp table. ADO.Net is unable to get the schema for the query. This is also why it works when you use the public temp table:- because the schema still exists when ADO looks for it.

If I find a solution, I'll let you know.

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old August 3rd, 2005, 09:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, I've come up with a work-round (which is probably better coding anyway). Instead of using a temp table, declare a table variable to hold the data. e.g.
Code:
DECLARE @PDropTemp TABLE([TagIndex] as <datatype>,[Timestamp] as <datatype>,[Drop] as <datatype>)
This doesn't seem to upset ADO and allows you do all the normal neat things with datasets etc.

HTH,

Chris

There are two secrets to success in this world:
1. Never tell everything you know
 
Old August 8th, 2005, 12:49 AM
Authorized User
 
Join Date: Aug 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris,

I perform my queries using three separate SqlCommand objects (and across a couple of function calls, since there's a bit of polymorphism involved), not all in one batch query or sproc.

How can I still use a table variable as a temporary storage location in this case?

Thanks

Eadred








Similar Threads
Thread Thread Starter Forum Replies Last Post
Temporary Tables Challenge! PCNHSL PHP How-To 1 November 8th, 2007 09:42 AM
creating temporary tables instead of queries clwk Access VBA 4 March 12th, 2007 02:50 PM
Crystal Report 8.0 - Working with Temporary Tables deebeedee VB How-To 0 July 27th, 2006 07:30 AM
Sql Server Temporary Tables itHighway SQL Server 2000 1 July 14th, 2005 12:33 AM
Urgent Help : XML to Sql temporary tables Milan XML 2 November 6th, 2004 07:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.