Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-21
6
Medium Priority
?
355 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

618 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