Solved

xp_cmdshell with variable

Posted on 2003-12-03
5
3,684 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:
ScottPletcher 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 extract information from SQL Server on Database, Connection and Server properties

706 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

20 Experts available now in Live!

Get 1:1 Help Now