January 24, 2008 at 1:52 am
Currently, I am tracking database structure changes through upgrade scripts. The database have a version number that each upgrade script update.
I would like to check, in the beginning of each upgrade script if the current version of database is correct.
The script look like this
USE Database
GO
DECLARE @CurrentVersion int,
@Msg varchar(255)
SELECT @CurrentVersion = dbo.GetVersion()
IF @CurrentVersion <> 2
BEGIN
SELECT @Msg = 'Unable to upgrade. Current version is '
+ CAST(@CurrentVersion as varchar(5))
RAISERROR (@Msg, 16, 1)
END
GO
ALTER FUNCTION
...
GO
CREATE PROCEDURE
...
GO
My question is: how can I stop or cancel the script execution when RAISERROR is throw?
Thanks
January 24, 2008 at 3:39 am
Backup database and if error happens during deployment restore the copy from backup.
_____________
Code for TallyGenerator
January 24, 2008 at 4:03 am
Hi
If u mean the Raiserror u hv in shown in ur code
use a GOTO whihc goes to the last line of your code.
How ever if u have other error handling in there u are better off using transactions. You will be able to rollback the transaction if any error occurs.
"Keep Trying"
January 24, 2008 at 5:21 am
Thanks for reply,
That was my first solution, but if you use command GO, you can't refer to label out the scope, that is, defined after GO command. The scope of commands is delimited by command GO.
If quit command GO, I can't use DDL commands, like ALTER FUNCTION and others.
January 24, 2008 at 12:38 pm
Did you try to run scripts from osql?
_____________
Code for TallyGenerator
January 25, 2008 at 1:27 am
Sergiy (1/24/2008)
Did you try to run scripts from osql?
Yes, but have the same result; the script continuous running after raiseerror.
January 25, 2008 at 5:07 am
You must have missed parameter -b.
_____________
Code for TallyGenerator
January 25, 2008 at 5:14 am
what about using transactions ? then you can use GOTO .
"Keep Trying"
January 25, 2008 at 5:24 am
Chirag (1/25/2008)
what about using transactions ? then you can use GOTO .
OP already answered this question/
"If quit command GO, I can't use DDL commands, like ALTER FUNCTION and others."
CREATE VIEW/FUNCTION/PROCEDURE statement must be 1st statement in a batch.
_____________
Code for TallyGenerator
February 1, 2008 at 5:42 am
Hi
can we use sp_executesql for all the procs/functions and put them inside a transaction.
also have a try/catch.
"Keep Trying"
Viewing 10 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply