SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


try..catch and union all


try..catch and union all

Author
Message
rightontarget
rightontarget
SSChasing Mays
SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)

Group: General Forum Members
Points: 649 Visits: 475
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,
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63570 Visits: 17966
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.

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)
rightontarget
rightontarget
SSChasing Mays
SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)

Group: General Forum Members
Points: 649 Visits: 475
Thanks for reply. By fails, I mean, the statement returns an error. Usually timeout error.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63570 Visits: 17966
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.

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)
rightontarget
rightontarget
SSChasing Mays
SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)SSChasing Mays (649 reputation)

Group: General Forum Members
Points: 649 Visits: 475
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?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63570 Visits: 17966
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.

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)
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18101 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search