473,544 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7738
br***********@i nfotechnics.co. uk (Brian) wrote in message news:<17******* *************** ****@posting.go ogle.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_lna me,@au_fname,@p hone,@address,
@city,@state,@z ip,@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
2733
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
3662
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 upgrade_sal () LANGUAGE SQL BEGIN DECLARE eldest INT;
6
4717
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 appreciated. Thanks in advance
3
6873
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 from rsSource into rsDest - if it finds a key violation then it deletes the current record from rsDest and adds the new record from rsSource. This...
13
4441
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 runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of...
6
2340
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 parameters. It returns my error text message in another output parameter as "ok", which is the value that is set in the stored procedure prior to doing the...
2
3862
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 visible so the document can be printed and/or modified. This all takes place within one form, in which the Word.Application and Word.Document objects...
9
2095
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 | E_PARSE); set_error_handler("SendErrorReport"); function SendErrorReport($errorNumber, $errorMessage, $errorFile, $errorLine, $vars) {
0
1812
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 is, I first drop it, and then recreate it. However when I try to create such stored procedure DB2 complains if the table already exists. The stupid...
0
7429
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7777
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5301
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3420
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.