try..catch and union all

  • Hi all,

    I have the following construct:

    select name, value

    from

    ( select name, value from view1

    union all

    select name, value from view2

    union all

    select name, value from view3

    )

    If any of the selects in the from fails, I need to display: 'no_data',0

    When I try to wrap each select in try/catch, SS screams at me.

    What are my options?

    Thanks,

  • eugene.pipko (10/29/2012)


    Hi all,

    I have the following construct:

    select name, value

    from

    ( select name, value from view1

    union all

    select name, value from view2

    union all

    select name, value from view3

    )

    If any of the selects in the from fails, I need to display: 'no_data',0

    When I try to wrap each select in try/catch, SS screams at me.

    What are my options?

    Thanks,

    You can't just toss try/catch block in the middle of a query. What do you mean by "If any of the selects in the from fails"? Are you expecting an error when selecting from a view? Or do you mean "fail" because there are no rows returned?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thanks for reply. By fails, I mean, the statement returns an error. Usually timeout error.

  • eugene.pipko (10/29/2012)


    Thanks for reply. By fails, I mean, the statement returns an error. Usually timeout error.

    You need to handle that in the front end. Your statement needs to complete before a try/catch will help you with that. A timeout is not a sql error, it is a connection error.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • What about any other error? There is no front end. The result is emailed to the user.

    I guess what I am trying to do is to let the other 2 parts of the union all work and report 'no_data',0 for the part that failed.

    Is it possible is SS 2K8R2?

  • eugene.pipko (10/29/2012)


    What about any other error? There is no front end. The result is emailed to the user.

    I guess what I am trying to do is to let the other 2 parts of the union all work and report 'no_data',0 for the part that failed.

    Is it possible is SS 2K8R2?

    The front end doesn't have to be visual. Something gets the results and emails it. Given what you are describing I wouldn't bother trying to capture other types of errors. Let what process runs your query handle it. Put your try/catch there and you should be good to go. It sounds like it doesn't matter if the connection times out or the database has been deleted, you want to send 'no_data'.

    Remember that a union is a single query. Parts of it can't work while other parts fail. It is all or nothing.

    select 1

    union all

    select 1 where 1 = 'x' --this query would fail

    Consider the above code. It is a simple query with a union. The second query will cause the whole thing to fail. If you want to capture row counts for each query they need to be run separately. There is no way to know from the results which query in a union returns results.

    Perhaps if you can explain the whole process a little more clearly I can see if I can help figure out a way to get what you want.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • eugene.pipko (10/29/2012)


    What about any other error? There is no front end. The result is emailed to the user.

    I guess what I am trying to do is to let the other 2 parts of the union all work and report 'no_data',0 for the part that failed.

    Is it possible is SS 2K8R2?

    My suggestion:

    1. INSERT each of the SELECT FROM view statements into a Temp table.

    2. Surround each with a TRY/CATCH.

    3. Set a flag in each CATCH to tell you which query failed.

    4. SELECT out of the temp table the results that were returned.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 7 posts - 1 through 6 (of 6 total)

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