Try Convert causes change in transaction state

  • Why does this code (below) cause a change in the transaction state?

    DECLARE @date DATE,

    @string VARCHAR(5)= 'test'

    BEGIN TRAN

    PRINT XACT_STATE()

    BEGIN TRY

    -- see if the data I've been given can be cast to something else

    SET @date = CAST(@String AS DATE)

    END TRY

    BEGIN CATCH

    PRINT XACT_STATE()

    END CATCH

    ROLLBACK TRAN

  • Varchar (5) that's why.

    The below does not throw an error.

    dECLARE @date DATE,

    @string VARCHAR(10)= '2015-01-01'

    BEGIN TRAN

    PRINT XACT_STATE()

    BEGIN TRY

    -- see if the data I've been given can be cast to something else

    SET @date = CAST(@String AS DATE)

    END TRY

    BEGIN CATCH

    PRINT XACT_STATE()

    END CATCH

    ROLLBACK TRAN

    Catch-all queries done right [/url]
    Gail Shaw's Performance Blog[/url]

  • I don't think you quite understand the issue. I'm attempting to parse a non-date into a date variable. Yes this sounds stupid, but I'm testing whether or not it can. If soemthing can't parse (in my original code) I substitute the value for something else.

    Basically I'm trying to recreate the functionality of TryParse but in 2008 R2.

    And so that's what the example is about.

  • Quick suggestion, use TRY_CONVERT

    ๐Ÿ˜Ž

    DECLARE @DATEVAR DATE = NULL;

    DECLARE @STRVAR VARCHAR(50) = 'Not A Date';

    BEGIN TRAN

    PRINT XACT_STATE()

    BEGIN TRY

    SELECT @DATEVAR = TRY_CONVERT(DATE,@STRVAR)

    END TRY

    BEGIN CATCH

    PRINT XACT_STATE()

    END CATCH

    ROLLBACK TRAN

  • Unfortunately, as much as I'd love to use TRY_CONVERT(), we are stuck in the dark ages using 2k8R2

  • try using IsDate() and then return error codes if not.

    Catch-all queries done right [/url]
    Gail Shaw's Performance Blog[/url]

  • MadAdmin (3/17/2015)


    try using IsDate() and then return error codes if not.

    That can be flawed as it depends on your date format, eg doing

    SELECT ISDATE('31-01-2015')

    SELECT ISDATE('31/01/2015')

    On my machine both return False, even though they are valid UK date formats, however both these return TRUE

    SELECT ISDATE('01-31-2015')

    SELECT ISDATE('01/31/2015')

    At best there is a possible issue in using the ISDATE function, you also have to consider when converting is '01-02-2015' the 1st of Feb, or the 2nd Jan?

    However this code works on SQL 2012 and SQL 2008R2

    DECLARE @Date DATE

    DECLARE @StrVar VARCHAR(5) = 'Not a date'

    BEGIN TRAN

    BEGIN TRY

    SELECT @Date = CAST(@StrVar as DATE)

    END TRY

    BEGIN CATCH

    Print Error_Message ()

    Print XACT_STATE()

    END CATCH

    ROLLBACK TRAN

    You have to think that that conversion could be used to update a table or inserting data into a new table, so on an error you would want to roll back the whole transaction if it failed, wouldn't you?

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • If you want to mimic the TRY_PARSE or TRY_CONVERT functions that are in 2012, then you may have to resort to a CLR to accomplish this, as you cant use TRY...CATCH in a function, and ISDATE can create false positives (earlier post!!).

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • chris.rogers00 (3/16/2015)


    Why does this code (below) cause a change in the transaction state?

    DECLARE @date DATE,

    @string VARCHAR(5)= 'test'

    BEGIN TRAN

    PRINT XACT_STATE()

    BEGIN TRY

    -- see if the data I've been given can be cast to something else

    SET @date = CAST(@String AS DATE)

    END TRY

    BEGIN CATCH

    PRINT XACT_STATE()

    END CATCH

    ROLLBACK TRAN

    From BOL: "The current request has an active user transaction, but an error has occurred that has caused the transaction to be classified as an uncommittable transaction."

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 9 posts - 1 through 8 (of 8 total)

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