Solved

Enabling transactions

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 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