473,395 Members | 1,948 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,395 software developers and data experts.

Erro trapping question

I have a batch file that runs SQL Server scripts using commands like:

OSQL -Umyname -Pmypassword -iScript_01.sql -w200 -e -n
Consolidation.log


Script_01.sql will contain statements like:
Update SASI.AACT set schoolnum='071' where schoolnum in ('000',' ')
update SASI.AATD set schoolnum='071' where schoolnum in ('000',' ')
update SASI.AATP set schoolnum='071' where schoolnum in ('000',' ')
update SASI.ACHS set schoolnum='071' where schoolnum in ('000',' ')
update SASI.ACLS set schoolnum='071' where schoolnum in ('000',' ')

If one of those tables should not exist, how could I have it continue,
but hopefully the log would have a reference to the error?

I am experimenting, but I am unsuccessfull with something like:
BEGIN TRAN
select count(*) from sasi.aact --this could be an update
statement
if @@ERROR =208 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
err_handle:
return
commit Tran
Jul 20 '05 #1
1 1104

<Oa**************@yahoo.com> wrote in message
news:13**************************@posting.google.c om...
I have a batch file that runs SQL Server scripts using commands like:

OSQL -Umyname -Pmypassword -iScript_01.sql -w200 -e -n
Consolidation.log


Script_01.sql will contain statements like:
Update SASI.AACT set schoolnum='071' where schoolnum in ('000',' ')
update SASI.AATD set schoolnum='071' where schoolnum in ('000',' ')
update SASI.AATP set schoolnum='071' where schoolnum in ('000',' ')
update SASI.ACHS set schoolnum='071' where schoolnum in ('000',' ')
update SASI.ACLS set schoolnum='071' where schoolnum in ('000',' ')

If one of those tables should not exist, how could I have it continue,
but hopefully the log would have a reference to the error?

I am experimenting, but I am unsuccessfull with something like:
BEGIN TRAN
select count(*) from sasi.aact --this could be an update
statement
if @@ERROR =208 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
select count(*) from sasi.astu
if @@ERROR <> 0 GOTO err_handle
err_handle:
return
commit Tran


Error handling is rather awkward in MSSQL:

http://www.sommarskog.se/error-handling-I.html
http://www.sommarskog.se/error-handling-II.html

In your case, the easiest thing is probably to avoid the error by checking
if the table exists before trying to query it:

if object_id('sasi.aact') is not null and
objectproperty(object_id('sasi.aact'), 'IsTable') = 1
begin
.... -- do something here
end
else
begin
.... -- log to an error table
end

Simon
Jul 20 '05 #2

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

Similar topics

13
by: David Gray | last post by:
Greetings all, Quick newbie type question: I would like to be able to trap non-numerical data entered into a textbox via CTRL+C and/or Shift+Insert. I realise that this data can be...
4
by: Wugi | last post by:
I'm trying to find an equivalent of key-event trapping which is easy in QBasic. Several of my programs build up a geometric figure with two parameter line families, eg with two nested for..next...
0
by: Z GIRL | last post by:
Hi, I posted previously about trapping on the PostBack function exactly what submit button was clicked. I understand there is __EventArgument and __EventTarget hidden fields for a PostBack...
8
by: Pete | last post by:
I'm trying to improve my code so that when I open a recordset object, I can absolutely guarantee it is closed and is set = Nothing. I have read some old threads and they all say to use the...
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...
3
by: windandwaves | last post by:
Hi Gurus Does anyone know how I set the error trapping to option 2 in visual basic. I know that you can go to tools, options and then choose on unhandled errors only, but is there a VB command...
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...
1
by: tarcisio | last post by:
Pessoal, qdo eu mando rodar a pagina o VS 2003 aparece o seguinte erro: Erro While trying to run project : Unable to start debbuging web server. Debbuging failed because integred Windows...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.