TheInsider
Programmer
Hi,
I'm having difficulty deleting a file from within a stored procedure. I know there are at least two methods of achieving this...
The above code works if I log into SQL Server using Windows Authentication only and execute the stored proc within management studio. However, if I try to call the stored proc from a C# module using the SQL Server login account that I created, I get an error stating the the user doesn't have adequate permissions to run XP_CMDSHELL.
The second method of deleting a file is
Unfortunately, the latter code never deletes the file. The output from the print statements is:
I don't know what -2146828218 is supposed to be, but it obviously indicates the deletion failed. It appears that even using my Windows Authentication login, I don't have permission to use the FileSystemObject to delete files.
If I have a SQL Server login account called "webserver" that I use to call stored procs from C#, what's the easiest way to delete files from TSQL... using the least amount of tweaking security permissions?
And yes, I do want to do this deletion from TSQL and not ASP.NET / C#.
TIA
I'm having difficulty deleting a file from within a stored procedure. I know there are at least two methods of achieving this...
Code:
EXEC XP_CMDSHELL 'del C:\temp\myfile.txt'
The second method of deleting a file is
Code:
BEGIN
DECLARE @Result INT
DECLARE @FSO INT
EXEC @Result = sp_OACreate 'Scripting.FileSystemObject', @FSO OUTPUT
PRINT CONVERT(VARCHAR, @@ERROR) + ' : ' + CONVERT(VARCHAR, @Result)
EXEC @Result = sp_OAMethod @FSO, 'DeleteFile', NULL, 'C:\temp\myfile.txt', 1
PRINT CONVERT(VARCHAR, @@ERROR) + ' : ' + CONVERT(VARCHAR, @Result)
EXEC @Result = sp_OADestroy @FSO
PRINT CONVERT(VARCHAR, @@ERROR) + ' : ' + CONVERT(VARCHAR, @Result)
END
Code:
0 : 0
0 : -2146828218
0 : 0
(1 row(s) affected)
If I have a SQL Server login account called "webserver" that I use to call stored procs from C#, what's the easiest way to delete files from TSQL... using the least amount of tweaking security permissions?
And yes, I do want to do this deletion from TSQL and not ASP.NET / C#.
TIA