Solved

URGENT : Missing something basic about @@ERROR and OSQL.

Posted on 2006-11-21
6
345 Views
Last Modified: 2012-05-05
Hi.

I have a VERY old DOS application which creates 54 TAB files containing 1 tables' data per file.

The program also creates a text file which is used as a T-SQL script for osql.

The script deletes the data from the DB in bottom up (so no orphans are created in the relationships between the tables).

The script then bulk inserts new data into the tables in top down (so to not create orphans).

Finally, the script shrinks the datafiles.

When this works, it is working fine. Nearly 6 million rows in 54 tables takes up 2.3GB. All good.

Sometimes, the DOS data is invalid - VERY VERY occassionally - bad app - can't get it fixed - living with it by rewriting a web version using MS SQL!

I want to use a transaction to allow me to rollback if the import fails.

So, I have something like this (edited to show the issue)


------------EDITED SAMPLE OF T-SQL script------------
ALTER DATABASE [TripleCDataConversion]
SET  RECOVERY SIMPLE

BEGIN TRANSACTION

DELETE TripleCDataConversion.dbo.Distributors
DELETE TripleCDataConversion.dbo.Customers
DELETE TripleCDataConversion.dbo.CommentCodes

IF @@ERROR <> 0
 BEGIN
 PRINT 'Failed Import Startup'
 END
ELSE
 BEGIN
 PRINT 'Successful Import Startup'
 END

IF @@ERROR = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.CommentCodes
 FROM 'D:\CCC2SQL\CO.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =                136,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  PRINT 'Failed to import CommentCodes         '
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported CommentCodes         '
  END
 END

IF @@ERROR = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.Customers
 FROM 'D:\CCC2SQL\CU1.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =               1790,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  PRINT 'Failed to import Customers            '
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported Customers            '
  END
 END

IF @@ERROR = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.Distributors
 FROM 'D:\CCC2SQL\DI.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =               1458,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  PRINT 'Failed to import Distributors         '
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported Distributors         '
  END
 END

IF @@ERROR = 0
 BEGIN
 COMMIT TRANSACTION
 PRINT 'Import successful'
 END
ELSE
 BEGIN
 ROLLBACK TRANSACTION
 PRINT 'Failed Import finalization'
 END

DBCC SHRINKDATABASE ([TripleCDataConversion], 0)

ALTER DATABASE [TripleCDataConversion]
SET  RECOVERY FULL
------------EDITED SAMPLE OF T-SQL script------------

The command to get this all working is ...

OSQL -U sa -P xxx -S yyy -d TripleCDataConversion -i D:\CCC2SQL\CCC_BULK.SQL -o D:\CCC2SQL\CCC_BULK.LOG -n -b -e

The log file for the time it all went wrong is ...


------------EDITED SAMPLE OF OSQL output------------
(1457 rows affected)
(1789 rows affected)
(136 rows affected)
Successful Import Startup
(136 rows affected)
Successfully imported CommentCodes
Msg 4864, Level 16, State 1, Server BANDACCESS, Line 1
Bulk insert data conversion error (type mismatch) for row 136, column
120 (NarrativeChangedDateTime).
Failed to import Customers
(1458 rows affected)
Successfully imported Distributors
Import successful
 DbId   FileId CurrentSize MinimumSize UsedPages   EstimatedPages
 ------ ------ ----------- ----------- ----------- --------------
     41      1      356672      131072      356544         356544
     41      2        2560        2560        2560           2560

(2 rows affected)
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
------------EDITED SAMPLE OF OSQL output------------


As you can see the Bulk Insert for Customers failed. The message is only printed because @@ERROR <> 0.

But, a few lines later, I re-test @@ERROR and it is now 0! So the import continues with the next table and then commits the whole lot.

Why is @@ERROR getting reset? Can you only "look" at it once?

How do I get this working?

I COULD nest the bulk inserts, but this would be 54 levels deep and I suspect that wouldn't work.

HELP!

Richard Quadling.
0
Comment
Question by:RQuadling
  • 3
  • 3
6 Comments
 
LVL 28

Accepted Solution

by:
imran_fast earned 500 total points
ID: 17986968
Hi try this using lables and parameter

ALTER DATABASE [TripleCDataConversion]
SET  RECOVERY SIMPLE

BEGIN TRANSACTION

Declare @Error int
set @Error = 0
DELETE TripleCDataConversion.dbo.Distributors
DELETE TripleCDataConversion.dbo.Customers
DELETE TripleCDataConversion.dbo.CommentCodes

IF @@ERROR <> 0
 BEGIN
 set @Error = 1
 PRINT 'Failed Import Startup'
 goto failed
 END
ELSE
 BEGIN
 PRINT 'Successful Import Startup'
 END

IF @Error = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.CommentCodes
 FROM 'D:\CCC2SQL\CO.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =                136,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  set @Error = 1
  PRINT 'Failed to import CommentCodes         '
  Goto Failed
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported CommentCodes         '
  END
 END

IF @Error = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.Customers
 FROM 'D:\CCC2SQL\CU1.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =               1790,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  set @Error = 1
  PRINT 'Failed to import Customers            '
  goto Failed:
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported Customers            '
  END
 END

IF @Error = 0
 BEGIN
 BULK INSERT TripleCDataConversion.dbo.Distributors
 FROM 'D:\CCC2SQL\DI.TAB'
 WITH
  (
  FIELDTERMINATOR = '\t',
  KEEPIDENTITY,
  KEEPNULLS,
  LASTROW =               1458,
  ORDER (UniqueID ASC)
  )
 IF @@ERROR <> 0
  BEGIN
  set @Error = 1
  PRINT 'Failed to import Distributors         '
  Goto Failed
  END
 ELSE
  BEGIN
  PRINT 'Successfully imported Distributors         '
  END
 END

IF @Error = 0
 BEGIN
 COMMIT TRANSACTION
 PRINT 'Import successful'
 END
ELSE
 BEGIN
 Failed:
 ROLLBACK TRANSACTION
 PRINT 'Failed Import finalization'
 END

DBCC SHRINKDATABASE ([TripleCDataConversion], 0)

ALTER DATABASE [TripleCDataConversion]
SET  RECOVERY FULL
0
 
LVL 40

Author Comment

by:RQuadling
ID: 17987019
DOH!

Thank you!

DOH! DOH! DOH!!!!!!!!!

0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17987041
If worked then close the Question

Regards

Imran
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 40

Author Comment

by:RQuadling
ID: 17987052
In my small test, this has worked. Will need to wait until overnight run though for full testing. But can't see it being an issue.
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17987097
Ok !
0
 
LVL 40

Author Comment

by:RQuadling
ID: 17994204
Easy points! All working fine. I even broke the data on purpose to test it!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now