Solved

Enabling transactions

Posted on 2004-04-06
4
373 Views
Last Modified: 2008-02-01
SET XACT_ABORT ON

BEGIN TRAN

Declare @mysql nvarchar(3000)

Set @mysql = "insert into..."
Exec sp_executesql @mysql
IF (@@ERROR <> 0)
BEGIN
      GOTO tran_failure_block
END

Set @mysql = "delete from..."
Exec sp_executesql @mysql
IF (@@ERROR <> 0)
BEGIN
      GOTO tran_failure_block
END

Set @sql = 'Exec master..xp_cmdshell (Runs a BCP In/Out here)'
Exec (@sql)
IF (@@ERROR <> 0)
BEGIN
      GOTO tran_failure_block
END

COMMIT TRAN
RETURN

tran_failure_block:
BEGIN

      ROLLBACK TRAN      
      RETURN
END


GO

This is the meat of my stored procedure. Anyways, the problem is with
the last command (xp_cmdshell) which I use to run BCP in and out operations.
Now, the problem is, if there is any error in the BCP, I can't catch it in
the stored proc, as I would imagine, BCP runs out of process from sql server.
I really want everything to go, or nothing to go and if there any errors in my
xp_cmdshell bcp, I want to catch it and rollback the entire transaction.

Any ideas???

0
Comment
Question by:dbdoshi
[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
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 10765776
For a bcp in , you could check  the number of rows in the target table before and after
For a bcp out, you could check file existence and creation date with another xp_cmdshell
or

xp_fileexists in SQL 2000

for more info on checking file existence, read
http://www.nigelrivett.net/CheckIfFileExists.html


HTH

Hilaire
0
 
LVL 13

Expert Comment

by:danblake
ID: 10766253
Or alternativly create an error file with bcp -e <file_name> and bulk insert into a temporary table and check if there are any errors.

also, why not use sp_Executesql rather than EXEC :
http://www.nigelrivett.net/sp_executeSQL.html

(This demonstrates the error trapping used)
0
 

Author Comment

by:dbdoshi
ID: 10766607
The problem with EXEC or sp_executesql is that I am not running anything native in sql server. Basically bcp is a cmd line uitlity, so if any errors are thrown, they will be thrown at command line, and won't propogate up to the sql server level. Thats my understanding of the communincations and error handling.
0
 
LVL 13

Accepted Solution

by:
danblake earned 250 total points
ID: 10766677
Try the following (demo):
--DROP TABLE #xp_cmdshell
CREATE TABLE #xp_cmdshell(drive varchar(1000), [MB Free] varchar(1000))

INSERT #xp_cmdshell(drive, [MB Free])
   EXEC master..xp_fixeddrives

select * from #xp_cmdshell


Have you tried it as:

--DROP TABLE #xp_cmdshell
CREATE TABLE #xp_cmdshell(results varchar(8000))

INSERT #xp_cmdshell(results)
   EXEC master..xp_cmdshell 'Dir C:\'

select * from #xp_cmdshell


-------- You can then also use your bcp xp_cmdshell resutls and the / -e error bcp output-file to check the results of the process.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running total between 2 sql tables in Sql 6 50
CAST issue with SQL 2000 15 44
how to solve "recursive" database tables 2 53
Need split for SQL data 5 37
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

737 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