0

I'm not getting success with this command

DECLARE @cmd sysname, @var sysname
SET @var = 'Hello world'
SET @cmd = 'echo ' + @var + ' > C:\var_out.txt'
EXEC master..xp_cmdshell @cmd

Already tried several examples, but it did not work. The command runs but the file simply does not create

tshepang
  • 12,111
  • 21
  • 91
  • 136
msantiago
  • 346
  • 2
  • 4
  • 14
  • Are you looking for the file on your machine or on the SQL box? – Dave Sexton Jul 23 '13 at 13:06
  • Does the account used for xp_cmdshell (normally the SQL Server Service account) have permission to write there? I would guess not. – jpw Jul 23 '13 at 13:07
  • Where do you expect this file could be created? It will be created on the SQL SERVER local drive c:\ not on the client PC. – valex Jul 23 '13 at 13:08

2 Answers2

1

What account is your SQL Server running under? Maybe it doesn't have enough privileges. If this is true, you can use sp_xp_cmdshell_proxy_account instead.

However, I believe you are trying to find the file on your machine instead of server. And what you are doing is storing the file to the server. Try to do it on the local SQL Server.

OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
-1

You must enable some settings before execute this script with sp_xp_cmdshell

USE [master]
GO

sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
andy
  • 1