|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Today @ 8:32 AM
Points: 39,
Visits: 227
|
|
I'm trying to write an error handling query using the TRY...CATCH construct as a homework problem. I want to compare today's day to a variable @CurrentDay equal to a day of 24 and have it fail out of the TRY construct to the CATCH construct and print a message indicating the actual day.
Is it possible to make my query work? If it is, what changes do I need to make my query work?
DECLARE @CurrentDay AS INT = 24;
BEGIN TRY SELECT @CurrentDay = DAY(CURRENT_TIMESTAMP) PRINT 'Today is the 24th day of the month'; END TRY BEGIN CATCH PRINT 'Today is day number ' + DAY(CURRENT_TIMESTAMP) + ' of the month'; END CATCH
Nicholas
"Nicholas"
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 5:52 PM
Points: 960,
Visits: 1,921
|
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Saturday, March 30, 2013 12:00 PM
Points: 82,
Visits: 348
|
|
Like Luis stated, use an if/else. If it fails, it'll fall to the "else" part and you can produce your message. Try/catch would normally be used to trap any unforeseen errors.
Mark
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Today @ 8:32 AM
Points: 39,
Visits: 227
|
|
I decided to oust that code. I needed to do a homework problem using TRY/CATCH.
Thank you for all suggestions.
"Nicholas"
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:06 AM
Points: 223,
Visits: 1,137
|
|
TRY/CATCH is traditionally used more for stuff like this:
SET NOCOUNT ON; DECLARE @pos TABLE (sale_id varchar(4), product_id int) DECLARE @err varchar(200)
BEGIN TRY INSERT INTO @pos VALUES ('ccc', 'a') END TRY BEGIN CATCH -- update the log file SET @err=ERROR_MESSAGE()
IF ERROR_NUMBER() = 245 SET @err = @err+'(Still getting bad data from Joe''s ETL)'
PRINT @err END CATCH
-- AJB xmlsqlninja.com
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: 2 days ago @ 6:54 PM
Points: 721,
Visits: 1,375
|
|
5280_Lifestyle (8/13/2012)
I'm trying to write an error handling query using the TRY...CATCH construct as a homework problem. I want to compare today's day to a variable @CurrentDay equal to a day of 24 and have it fail out of the TRY construct to the CATCH construct and print a message indicating the actual day. Is it possible to make my query work? If it is, what changes do I need to make my query work? DECLARE @CurrentDay AS INT = 24;
BEGIN TRY SELECT @CurrentDay = DAY(CURRENT_TIMESTAMP) PRINT 'Today is the 24th day of the month'; END TRY BEGIN CATCH PRINT 'Today is day number ' + DAY(CURRENT_TIMESTAMP) + ' of the month'; END CATCH
Nicholas
If your homework isn't overdue yet, I suggest that you should investigate using an IF construct in your TRY block in conjunction with RAISERROR to generate an error of sufficient severity to initiate error handling (i.e., shift control to the CATCH block) when the date you are testing does not fall within the required value(s). That should get you started, but if you run into problems, post the code you've tried and we'll see what we can do.
|
|
|
|