Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

try..catch and union all Expand / Collapse
Author
Message
Posted Monday, October 29, 2012 12:48 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:51 PM
Points: 89, Visits: 300
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,


Post #1378416
Posted Monday, October 29, 2012 12:55 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:44 PM
Points: 11,970, Visits: 10,995
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1378421
Posted Monday, October 29, 2012 1:05 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:51 PM
Points: 89, Visits: 300
Thanks for reply. By fails, I mean, the statement returns an error. Usually timeout error.
Post #1378427
Posted Monday, October 29, 2012 1:13 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:44 PM
Points: 11,970, Visits: 10,995
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1378432
Posted Monday, October 29, 2012 2:12 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, April 14, 2014 2:51 PM
Points: 89, Visits: 300
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?
Post #1378459
Posted Monday, October 29, 2012 2:42 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:44 PM
Points: 11,970, Visits: 10,995
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1378470
Posted Tuesday, October 30, 2012 2:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:03 PM
Points: 3,590, Visits: 5,098
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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1378609
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse