Solved

Return error messages from xp_cmdshell without using temp tables

Posted on 2007-11-15
6
244 Views
Last Modified: 2008-02-01
Hi Guys n Gals,

I've been asked to develop an SP that can run dos commands and isqlw. The SP has to return any error messages from the operating system or SQL Server. But I can't create temporary tables in the SP to store the error messages.  The bare bones SP looks like this so far ... any ideas?

Cheers,

Pete

-- This SP is for running xp_cmdshell and reporting back and operating system error messages
CREATE proc sp_RunCmd
 @MyCmd varchar (50), @Result varchar(250) OUTPUT
AS
-- @MyCmd - This will be a DOS cmd or isqlw statement e.g. @MyCmd = 'mkdir MyDir ' Or what ever

-- Normally I'd do  this but we can't create temp tables
CREATE TABLE #TempTable(Message VARCHAR(250))       -- can't do this
INSERT #TempTable                               
EXEC master.dbo.xp_cmdshell @MyCmd      
            
SELECT @Result = Message FROM #TempTable            
                        


0
Comment
Question by:Pete_Burke
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20287836
>CREATE TABLE #TempTable(Message VARCHAR(250))       -- can't do this
why not?

0
 

Author Comment

by:Pete_Burke
ID: 20287860
Its a replica database and read only
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20287879
then you need to choose some alternative:
* put your procedure into another database
* try to create table table in another database explicitely
  CREATE TABLE tempdb..#TempTable(Message VARCHAR(250))    

0
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 20287885
Hello Pete_Burke,

you can create temp tables as physically they are created in the tempdb. and there is no other way other than using temp tables / table variables

now the wn is , if this is a read only db, how can u create an sp there

Aneesh R.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20287898
Any Why you dont want to create #Temp tables? There is no other way round to deal with this is to redirect the output to a file and read that file from SP.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20287907
Sorry a statement error in my comments.
Any Why you dont want to create #Temp tables? There is no other way round to deal with this except that you redirect the output to a file and read that file from SP and parse the text.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

717 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