473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Trapping Errors in Stored Procedure

Hi there,

I am converting a large PL/SQL project into Transact-SQL and have hit
an issue as follows:

I have a PL/SQL procedure that converts a string to a date. The
procedure does not know the format of the date in the string so it
tries loads of formats in converting the string to a date until it
succeeds.

After trying each potential format it uses the Oracle 'EXCEPTION WHEN
OTHERS' construct to trap the failure so it can try another format.

Is it possible to do this with SQLServer ? If I do a CONVERT and it is
not one of the standard formats it fails. This is part of a background
scheduled process and I cannot afford the procedure to bomb out.

I suspect the answer is I cannot do this and will need to impose some
control over the string being received (from various external
systems!!) to ensure it is a specific known format. Even if I know it
will be one of the known SQLServer formats this will not be enough
since if the first one I try is not correct the process will crash.

Any ideas ?

Thanks
Jul 20 '05 #1
2 7731
br***********@infotechnics.co.uk (Brian) wrote in message news:<17**************************@posting.google. com>...
Hi there,

I am converting a large PL/SQL project into Transact-SQL and have hit
an issue as follows:

I have a PL/SQL procedure that converts a string to a date. The
procedure does not know the format of the date in the string so it
tries loads of formats in converting the string to a date until it
succeeds.

After trying each potential format it uses the Oracle 'EXCEPTION WHEN
OTHERS' construct to trap the failure so it can try another format.

Is it possible to do this with SQLServer ? If I do a CONVERT and it is
not one of the standard formats it fails. This is part of a background
scheduled process and I cannot afford the procedure to bomb out.

I suspect the answer is I cannot do this and will need to impose some
control over the string being received (from various external
systems!!) to ensure it is a specific known format. Even if I know it
will be one of the known SQLServer formats this will not be enough
since if the first one I try is not correct the process will crash.

Any ideas ?

Thanks


For the most part T-SQL doesn't stop for errors. If you want to trap
errors you need to periodically check the value of @@ERROR. Here's an
example from Microsoft's books online:
-- Execute the INSERT statement.
INSERT INTO authors
(au_id, au_lname, au_fname, phone, address,
city, state, zip, contract) values
(@au_id,@au_lname,@au_fname,@phone,@address,
@city,@state,@zip,@contract)

-- Test the error value.
IF @@ERROR <> 0
BEGIN
-- Return 99 to the calling program to indicate failure.
PRINT "An error occurred loading the new author information"
RETURN(99)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT "The new author information has been loaded"
RETURN(0)
END
GO

Dave
Jul 20 '05 #2
[posted and mailed, please reply in news]

Brian (br***********@infotechnics.co.uk) writes:
I am converting a large PL/SQL project into Transact-SQL and have hit
an issue as follows:

I have a PL/SQL procedure that converts a string to a date. The
procedure does not know the format of the date in the string so it
tries loads of formats in converting the string to a date until it
succeeds.

After trying each potential format it uses the Oracle 'EXCEPTION WHEN
OTHERS' construct to trap the failure so it can try another format.

Is it possible to do this with SQLServer ? If I do a CONVERT and it is
not one of the standard formats it fails. This is part of a background
scheduled process and I cannot afford the procedure to bomb out.

I suspect the answer is I cannot do this and will need to impose some
control over the string being received (from various external
systems!!) to ensure it is a specific known format. Even if I know it
will be one of the known SQLServer formats this will not be enough
since if the first one I try is not correct the process will crash.


SQL Server's error handling is nothing to write home about. Indeed,
syntax error in dates often causes batch-termination.

I have written two articles on error handling in SQL Server. You might
want to start with the second of them:
http://www.algonet.se/~sommar/error-handling-II.html.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Walter | last post by:
Hi, I have a stored proc StoredProc1 = { INSERT INTO Table1 SELECT * FROM View1 Return @@ERROR } StoredProc1 is used in another sp StoredProcMain =
1
by: DB_2 | last post by:
Hello, I created the following stored procedure. (Please ignore the logic in it; at this point it is not complete and I am trying to get the basic structure working first.) CREATE PROCEDURE...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Nathan Bloomfield | last post by:
Hi there, I am having difficulty with a piece of code which would work wonders for my application if only the error trapping worked properly. Basically, it works as follows: - adds records...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
6
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output...
2
by: Captain Nemo | last post by:
I'm still using Office 2000 myself, but some of my clients have Office 2003. I've recently added a piece of code to create an instance of Word, open a document, fill in the blanks and become...
9
by: 47computers | last post by:
Pretty new to PHP, I recently started learning about error trapping. As of right now, I include the following into a page in my website: -------BEGIN PASTE-------- error_reporting(E_ERROR |...
0
by: mersis | last post by:
I have a very very annoying problem. I want to create a stored procedure, that creates a table and does various things with it. Before creating a stored procedure I check if the table is there. If it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.