tags:

views:

50

answers:

2

I mean hypothetically is it possible to control query execution?

For example I got a big query and it does a lot of things but suddenly it gets an error, but I don't want to stop its execution, I just wanna skip that step and continue further.

Or I want to let the user know what's going on, what's actually is happening on the server right now.

Can I have some feedback from Sql server? Like "Just deleted the trigger successfully" or "I just Screwed with table alternation... So-n-so"

+1  A: 

You could try breaking your stored procedure up into several smaller pieces and wrapping them all in a transaction.

TLiebe
No.. I mean how to control sql script's runtime from your c# code?
Ike
That's not exactly a storeproc. I'm trying to run a script generated by Management Studio's wizard from my c# code to create a database based on that script. Sometimes for some reason it throws an exception. But I'd like to control the script's lifetime and skip those problematic pieces and also I'd like to inform user in my app's log which part of the script (actual t-sql statement) is working right at the moment.Theoretically it's possible, right?
Ike
If you modify the script that the wizard generated you could insert a variable and update it periodically so you can track the progress. If you insert code to check for errors (use @@ERROR) after every statement in your script, you can exit the script and pass back the error and the progress variable to the calling program. As for reporting progress, the best way I can think to do that is to break up the script from the wizard into several smaller pieces, call them sequentially and report the progress back to the user after each piece is executed.
TLiebe
Yeah I split entire script to smaller parts. Didn't know that GO command is actually not exactly t-sql command. I was surprised at first why Database.ExecuteNonQuery() takes the entire script without problems, but similar SqlCommand.ExecuteNonQuery() doesn't.
Ike
A: 

Sure you can. Use

SAVE { TRAN | TRANSACTION } { savepoint_name | @savepoint_variable }
[ ; ]

http://msdn.microsoft.com/es-es/library/ms188378.aspx

With saved points, you can have grain control over which statements commit or not.

And at the end, you can return this information to your app by out variables or a table with information.

Fraga
I mean if I try to run a script that was generated by SQL Management script wizard, from my c# code using something like ExecutNonQuery or something else. How can I control it?
Ike