I’ve got a very long query for our SQL Server 2000 database, and I am returning the results for further processing by my C# application.
Consider this query:
declare @sn varchar(20)
select distinct serial_number
from manufacturingData
where '7/19/2012'<date_time
order by serial_number
while (0 < (select count(serial_number) from #sn)) begin
select top 1 @sn=serial_number from #sn
exec sp_GetSnData @sn
delete from #sn where serial_number=@sn
end
drop table #sn
I am writing this query on 07/19/2012, so it is only returning records from today.
The query above (for half of today), returned 119 distinct serial numbers and took 52 seconds to execute with the while loop that accompanies it.
Typically, this report is executed for a 30-day period, and there are around 3500 part numbers.
Is there a way to “hook into” the result set so that I can have access to the results as they are generated?
The way it is now, no one knows if the query is going to take 2 minutes or 2 days …or if it is even still doing anything! Does the manager shut down his computer and go home for the day or is this report query just about done?
Once the individual tables are returned, then I need to process each table individually.
It would be nice if I could
- find out how many distinct items were returned,
- what item is currently being queried, and
- have the resulting table available to process while waiting for the next table to come in.
Is this possible?
FYI: I currently do this in my Windows Form, by sending individual requests to our Server, but this is bogging the Server down and using up all of the available connections.
Well based on your comments I’d be tring to optimise the stored proc.
But if you just want to give some quick and simple indication of how long it will take
just do
and multiply it by some fiddle factor say .5 second based on your rough performance figures.
If you want progress, then have a look at sql server DMO
Basically you put a Print Statement inside your loop “Processing 1 of 119 say”. SMO will pick up the output and trigger an event, you add a handler to drive a say a progress bar.
Note DMO works with 2000, and you can get a backwards compatibility pack so it will work with 2005 and 2008. It does (at the moment also work with 2012), but it’s not supported and you’d be mad to rely on it continuing to do so. From 2005 DMO was replaced with SMO.
Me I’d be working on optimsising the sp first though for any quick wins.