[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

xp_cmdshell with variable

Posted on 2003-12-03
5
Medium Priority
?
4,000 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 164 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 70

Accepted Solution

by:
Scott Pletcher earned 172 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 164 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

656 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