Link to home
Start Free TrialLog in
Avatar of bakerOSU333
bakerOSU333

asked on

Is it possible to use a progress bar in C# to track a running SQL stored procedure?

I'm trying to display the progress of a stored procedure that is called from C# app code and running in SQL Server 2005.  Is it possible to track how much of the said stored procedure is complete, doesn't have to be exact, just estimated?  I have the stored procedure code below and it is used to completely delete all data from a database and usually takes betwee 4 and 10 minutes to run depending upon how much data is in it.
USE [myDB]
GO
 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE PROCEDURE [dbo].[sbs_ClearAllData]
 
AS
BEGIN
 
	SET NOCOUNT ON;	
 
	-- Clear out tables
	EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
	EXEC sp_MSForEachTable  
	'IF OBJECTPROPERTY(object_id("?"), "TableHasForeignRef") = 1  
		DELETE FROM ?  
	else   
		TRUNCATE TABLE ?'
	EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
	EXEC sp_MSForEachTable  
	'IF OBJECTPROPERTY(object_id("?"), "TableHasIdentity") = 1   
	DBCC CHECKIDENT ("?", RESEED, 0)'
 
	-- Populate a couple of the tables.
	INSERT INTO myTable1
		(Id, Code, Description, IsCredit)
	VALUES
		(NEWID(), 'D', 'Debit', 0)
	INSERT INTO myTable2
		(Id, Code, Description, IsCredit)
	VALUES
		(NEWID(), 'C', 'Credit', 1)
 
END

Open in new window

Avatar of Aneesh
Aneesh
Flag of Canada image

you can try querying the no of entries on myTable1 and  myTable2 (Make sure you put  'NOLOCK' hints ) ,
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And, probably easier to create another thread and query the tables as aneesh has said.... You will know how many have gone, but not how many yet to do... Might need to do a count first.
No, you cannot do that.
Hi acperkins, which bit, or all the above ?
Yeah it was a bit cryptic.  Let's try that again:
>>Is it possible to track how much of the said stored procedure is complete, doesn't have to be exact, just estimated? <<
You cannot get any feedback from a running Stored Procedure.  The best you can do as suggested is calculate the number of rows affected before executing the Stored Procedure and estimate the time it will take.  But then you are, what could be called in the realm of the Heisenberg uncertainty principle.
Another method, which may (not) work is to record the amount of time it takes to execute in a procedure performance table.  Then do an estimate of time remaining based upon average execution time.
Avatar of bakerOSU333
bakerOSU333

ASKER

First off, thanks for all the input.  Let me run this idea by you guys... So how could alter that stored procedure to do the following:  (1) before running the procedure, what command could I pass to SQL to the total count of user tables in the database?  (2) then how would I alter the stored procedure I have above to just delete the data one table at a time?  (then I could use my progress bar to just loop through all the tables and it may lag in some of the bigger tables, but it would be good enough for my stuff)  Also, I still want to be able to delete all the data, regardless of foreign keys and such, but keep the schema and integrity...