A Simple way of Automating Scripts

  • Is their a way to handle begin tran statement when executing script thru sqlcmd...

    Regards,
    [font="Verdana"]Sqlfrenzy[/font]

  • Sqlfrenzy (8/11/2009)


    Is their a way to handle begin tran statement when executing script thru sqlcmd...

    Yep, you could use a transaction like this:

    begin tran

    :r "C:\sqlscripts\01.script1.sql"

    if (@@error != 0) goto endRollback

    :r "C:\sqlscripts\02.script2.sql"

    if (@@error != 0) goto endRollback

    print 'commit'

    commit tran

    goto endFinal

    endRollback:

    print 'rollback'

    rollback tran

    endFinal:

    print 'endFinal'

  • Since SQLCMD is a SQL Server 2005 tool you may want to instead use the TRY/CATCH that is available instead of the @@error. Maybe something like the following:

    BEGIN TRY

    BEGIN TRANSACTION;

    :r "C:\sqlscripts\01.script1.sql"

    :r "C:\sqlscripts\02.script2.sql"

    IF (XACT_STATE()) = 1 BEGIN

    PRINT 'COMMIT';

    COMMIT TRANSACTION;

    END;

    END TRY

    BEGIN CATCH

    IF (XACT_STATE()) 0 BEGIN

    PRINT 'ROLLBACK';

    ROLLBACK TRANSACTION;

    END;

    SELECT ERROR_NUMBER() [ErrorNumber]

    ,ERROR_SEVERITY() [ErrorSeverity]

    ,ERROR_STATE() [ErrorState]

    ,ERROR_PROCEDURE() [ErrorProcedure]

    ,ERROR_LINE() [ErrorLine]

    ,ERROR_MESSAGE() [ErrorMessage];

    END CATCH;

  • tpa (8/11/2009)


    Sqlfrenzy (8/11/2009)


    Is their a way to handle begin tran statement when executing script thru sqlcmd...

    Yep, you could use a transaction like this:

    begin tran

    :r "C:\sqlscripts\01.script1.sql"

    if (@@error != 0) goto endRollback

    :r "C:\sqlscripts\02.script2.sql"

    if (@@error != 0) goto endRollback

    print 'commit'

    commit tran

    goto endFinal

    endRollback:

    print 'rollback'

    rollback tran

    endFinal:

    print 'endFinal'

    actually I was trying to communicate that if there is a begin tran without any corresponding rollback or

    commit in the script to be executed...how this can be taken care of....

    Regards,
    [font="Verdana"]Sqlfrenzy[/font]

  • actually I was trying to communicate that if there is a begin tran without any corresponding rollback or

    commit in the script to be executed...how this can be taken care of....

    Regards,

    Sqlfrenzy

    Then maybe if you don't have the BEGIN TRANSACTION in the code I showed. Since it checks for the TRANSACTION state before executing the COMMIT or ROLLBACK I would think this could do what you need.

    BEGIN TRY

    --BEGIN TRANSACTION;

    :r "C:\sqlscripts\01.script1.sql"

    :r "C:\sqlscripts\02.script2.sql"

    IF (XACT_STATE()) = 1 BEGIN

    PRINT 'COMMIT';

    COMMIT TRANSACTION;

    END;

    END TRY

    BEGIN CATCH

    IF (XACT_STATE()) 0 BEGIN

    PRINT 'ROLLBACK';

    ROLLBACK TRANSACTION;

    END;

    SELECT ERROR_NUMBER() [ErrorNumber]

    ,ERROR_SEVERITY() [ErrorSeverity]

    ,ERROR_STATE() [ErrorState]

    ,ERROR_PROCEDURE() [ErrorProcedure]

    ,ERROR_LINE() [ErrorLine]

    ,ERROR_MESSAGE() [ErrorMessage];

    END CATCH;

    But why not just put the TRANSACTION there to begin with?

  • Or maybe use a while loop like this to check for all transactions and then commit or rollback until finished and none are found. Have never tried it this way so beware.:

    BEGIN TRY

    BEGIN TRANSACTION;

    :r "C:\sqlscripts\01.script1.sql"

    :r "C:\sqlscripts\02.script2.sql"

    WHILE (XACT_STATE()) = 1 BEGIN

    PRINT 'COMMIT';

    COMMIT TRANSACTION;

    END;

    END TRY

    BEGIN CATCH

    WHILE (XACT_STATE()) 0 BEGIN

    PRINT 'ROLLBACK';

    ROLLBACK TRANSACTION;

    END;

    SELECT ERROR_NUMBER() [ErrorNumber]

    ,ERROR_SEVERITY() [ErrorSeverity]

    ,ERROR_STATE() [ErrorState]

    ,ERROR_PROCEDURE() [ErrorProcedure]

    ,ERROR_LINE() [ErrorLine]

    ,ERROR_MESSAGE() [ErrorMessage];

    END CATCH;

  • Sqlfrenzy (8/11/2009)


    actually I was trying to communicate that if there is a begin tran without any corresponding rollback or

    commit in the script to be executed...how this can be taken care of....

    You should be able to check @@TRANCOUNT

    Martin

  • There was a smilar article published on 8/23/2010 http://www.sqlservercentral.com/articles/scripting/70783/

    but neither cases no discussions about how to handle error or script failure and notifications.

    Good article.

  • good

  • There is a solution to be able to deploy sql scripts from folder structure ... so that the dba would do a simple double-click .... regardless of the where the script is executed , to which server to which instance , database , etc.

    based on the simple principle :

    for /f %i in ('dir *.SQL /s /b /o') DO ECHO %DATE% --- %TIME% RUNNING %i 1>>"..\install.log"&SQLCMD -U sqlloginUserName -P SecretPass -H hostname -d cas_dev -t 30 -w 80 -u -p 1 -b -i %i -r1 1>> "..\install.log" 2>> "..\error.log"

    http://ysgitdiary.blogspot.com/2010/04/blog-post_26.html

  • @yordan.georgiev

    I checked your batch file on your blog but can you describe a little bit what and how is it doing?.

    Thanks.

    Don't just give the hungry man a fish, teach him how to catch it as well.

    the sqlist

  • Hi ,

    *** Don't just give the hungry man a fish, teach him how to catch it as well.

    In my opinion this is exactly what I did ; )

    Ok .. Download this project :

    http://ysgitdiary.blogspot.com/2010/10/cassqldev-out-now.html

    and check the Install/scripts section - it has the whole solution + Bonus for logging both selects and errors ; )

  • yordan.georgiev (11/25/2010)


    Hi ,

    *** Don't just give the hungry man a fish, teach him how to catch it as well.

    In my opinion this is exactly what I did ; )

    Sorry, man, you just gave me a fish in the form of a batch file which is very difficult to "decrypt" in terms of functionality. I also tried it and didn't work.

    I have a script like that which I built by myself but I am always willing to learn new tricks if possible. I only wanted to understand what you did.

    Thanks.

    Don't just give the hungry man a fish, teach him how to catch it as well.

    the sqlist

  • Thanks for the article.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

Viewing 15 posts - 31 through 45 (of 72 total)

You must be logged in to reply to this topic. Login to reply