Solved

Enabling transactions

Posted on 2004-04-06
4
367 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
  • 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

830 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