Solved

Enabling transactions

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now