Solved

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

Posted on 2009-05-15
8
2,063 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:bakerOSU333
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24398058
you can try querying the no of entries on myTable1 and  myTable2 (Make sure you put  'NOLOCK' hints ) ,
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 24398079
Hello bakerOSU333,

You could set up a MSMQ and try to have the stored procedure post a message into that queue, but you are using other stored procedured, so could be difficult.

Have a look at books on line for :

CREATE MESSAGE TYPE message_type_name
    [ AUTHORIZATION owner_name ]
    [ VALIDATION = {  NONE
                    | EMPTY
                          | WELL_FORMED_XML
                    | VALID_XML WITH SCHEMA COLLECTION
                                                    schema_collection_name
                   } ]
[ ; ]



Regards,

mark_wills
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 24398107
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.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24398261
No, you cannot do that.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:Mark Wills
ID: 24398287
Hi acperkins, which bit, or all the above ?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24398399
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.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24398597
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.
0
 

Author Comment

by:bakerOSU333
ID: 24398835
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...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now