Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 xp_cmdshell questions

Author  Topic 

johnsql
Posting Yak Master

161 Posts

Posted - 2008-02-12 : 15:18:49
Buddies,
I just copy an example scripts from BOL for xp_cmdshell, but the script fails with output as NULL and contents of the external files just contains word "time". What is wrong with that BOL example.

DECLARE @cmd sysname, @var sysname
SET @var = 'dir /p'
SET @cmd = 'echo ' + @var + ' > C:\dir_out.txt'
EXEC master..xp_cmdshell @cmd

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-02-12 : 15:29:47
The output is correct as NULL. This is not a failure. The external file should contain "dir /p" in it.

I tested the code on a SQL Server 2000 box and it worked as I have described.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

johnsql
Posting Yak Master

161 Posts

Posted - 2008-02-12 : 16:06:56
quote:
Originally posted by tkizer

The output is correct as NULL. This is not a failure. The external file should contain "dir /p" in it.

I tested the code on a SQL Server 2000 box and it worked as I have described.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/



Thanks for your reply.
However, the BOL online says "This example writes the contents of the current directory to a file named dir_out.txt in the current server directory." (topic: BOL > xp_cmdshell).

How can the writing happens to my external file?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-02-12 : 16:13:56
Their example does not do what they said, so it has a bug in it. They are only echo'ing the command to a file.

Use this instead (removed the echo):
DECLARE @cmd sysname, @var sysname
SET @var = 'dir /p'
SET @cmd = @var + ' > C:\dir_out.txt'
EXEC master..xp_cmdshell @cmd

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-12 : 17:31:57
How do the xp_cmdshell deal with "/p" option?
Doesn't the DIR command wait for a keypress?

Oh, I see now. The output to a file overrides the /p option.


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -