Solved

SQL 2008 | errors from xp_cmdshell execution

Posted on 2012-03-28
3
879 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

8 Experts available now in Live!

Get 1:1 Help Now