Solved

xp_cmdshell with variable

Posted on 2003-12-03
5
3,849 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
[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
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:Dishan Fernando
Dishan Fernando 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

691 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