Solved

Enabling transactions

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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

920 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

13 Experts available now in Live!

Get 1:1 Help Now