Solved

xp_cmdshell with variable

Posted on 2003-12-03
5
3,754 Views
Last Modified: 2012-06-21
this is my script:

BEGIN
DECLARE @ret int
DECLARE @instr sysname
SET @instr='dir'
EXEC @ret = xp_cmdshell @instr
UPDATE dirresults SET result=@ret WHERE id=20
END


the problem is, result always stays empty (even if i change my instruction to something that doesn't exist)

my questions:
a)how can I get the result (0 or 1)?
b) how can I get the result of the dir command (e.g. the directory listing)?
0
Comment
Question by:grotesmurf2
5 Comments
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9866279
? why have you declared your parameter as a sysname as opposed to varchar ?
0
 
LVL 8

Assisted Solution

by:dishanf
dishanf earned 41 total points
ID: 9866282
CREATE Procedure SampleSP
      /* Param List */
AS
      DECLARE @ERROR int      -- For Hold Error Number
      DECLARE @CMD      varchar(1000)      -- Dts Run Command
BEGIN
      -- Set as No Error
      SET @ERROR = 0
      SET @CMD = 'dir'      
      EXECUTE @ERROR = master..xp_cmdshell @CMD
      -- + Error Checking
      SELECT @ERROR = COALESCE( NULLIF ( @ERROR, 0 ), @@ERROR )
      IF @ERROR <> 0 BEGIN ROLLBACK TRANSACTION RETURN @ERROR END
END
      RETURN @ERROR
GO

this will return 0
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 43 total points
ID: 9867011
*) Make sure you have authority to run xp_cmdshell.  Unless you are on 95/98, you must be sysadmin or have explicit authority to run xp_cmdshell.

*) I seem to be able to get a rc of 0 or 1 from dir without a problem.  Not sure what is going on there, unless it's an authority issue.
*) You can capture the output into a temp table, for example:

CREATE TABLE #dir (dirLine NVARCHAR(255))
INSERT INTO #dir
EXEC @ret = master..xp_cmdshell 'dir' --, NO_OUTPUT
SELECT * FROM #dir
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 41 total points
ID: 9868896
the output of xp_cmdshell is nvarchar(255)
the return code will be either 0 success or 1 failure (unless win98 when you just get 0)
you don't get the errorlevel directly from the command...


try
  create table #t (tid integer identity(1,1) , cmddata nvarchar(255))
  Insert into #t (cmddata)
    exec  Master..xp_cmdshell @cmd

 select * from #t
0
 

Expert Comment

by:coolmints
ID: 9869021
thank you all...

it must be an authority issue I guess. strange I don't get 0 or 1 as output, but just an empty string... sigh...
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

832 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