Solved

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

Posted on 2006-11-21
6
346 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

914 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

12 Experts available now in Live!

Get 1:1 Help Now