Solved

SQL 2008 | errors from xp_cmdshell execution

Posted on 2012-03-28
3
901 Views
Last Modified: 2012-03-28
Hi,

I am using code below to extract data into flat files. How can I capture any errors out of this T-sql script and raise alerts?

--  xp_cmdshell to extract data
EXEC xp_cmdshell 'bcp "SELECT * FROM test1 " queryout "C:\test1.txt" -T -c -t,'
EXEC xp_cmdshell 'bcp "SELECT * FROM test2 " queryout "C:\test2.txt" -T -c -t,'
EXEC xp_cmdshell 'bcp "SELECT * FROM test3 " queryout "C:\test3.txt" -T -c -t,'

Many Thanks
0
Comment
Question by:crazywolf2010
  • 2
3 Comments
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37777065
Hi,
You can use the return status of the xp_cmdshell procedure (like below):
------
declare @retValue int
exec @retValue = xp_cmdshell 'bcp "SELECT * FROM test1 " queryout "C:\test1.txt" -T -c -t,'

if @i <> 0
begin
      raiserror 99997'error'
end
-------
0
 

Author Comment

by:crazywolf2010
ID: 37777107
Hi,
I do need to know the errortext and not just that error occured.
Can we copy everything which was sent within BCP process?

Thanks
0
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 500 total points
ID: 37777225
Yes, you can get the output/error text of xp_cmdshell.

declare @retValue int
create table #Temp1(OutputText varchar(100))
insert into #Temp1
exec @retValue = xp_cmdshell 'bcp "SELECT * FROM test1 " queryout "C:\test1.txt" -T -c -t,'

if @i <> 0
begin
      raiserror 99997'error'
                select * from #Temp1
end
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

827 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