?
Solved

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

Posted on 2006-11-21
6
Medium Priority
?
354 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:Richard Quadling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 28

Accepted Solution

by:
imran_fast earned 2000 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:Richard Quadling
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Author Comment

by:Richard Quadling
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:Richard Quadling
ID: 17994204
Easy points! All working fine. I even broke the data on purpose to test it!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

752 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