Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
SQL
/* example from help .. "Access is denied" along with a pesky "NULL" on another line */

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

Experimenting with the form:
EXEC xp_cmdshell '@echo SELECT [blahblah] FROM [dbo].[masterblah] WHERE [blahIdx] = 1 >> J:\blahOutput.txt'

Alternatives would be great. But ... this should work (been there, done it before).

Thanks
Posted
Comments
Richard MacCutchan 30-Dec-12 3:52am    
Don't try to create files in the root of your C: drive.

The chances are that the problem is just the destination of your file: Vista and above require full admin rights to write or modify any file in the root directory of your boot drive. The user under which SQL server is executing will not have that permission. Change the folder: either to a created folder which as "open" access for all users, or an existing data directory which does.
 
Share this answer
 
Found solution.

And I had done this before. Simple change of the service type. From "NETWORK SERVICES" to "LOCAL SYSTEM".

This can be done in Computer Managment console; "Log on As" tab of the Properties.
 
Share this answer
 
Comments
Zoltán Zörgő 31-Dec-12 14:06pm    
Not a good one. Read this article: http://thinkingeek.com/2008/11/13/controlling-the-commands-executed-with-xp_cmdshell-on-sql-server-2005/
RedDk 31-Dec-12 14:18pm    
... of note, also, is that ssmse execution requires dir /x path notation to be of any use here in this scenario. Aggrevating, yes?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900