Hello everybody!
In this post, I will demonstrate how to perform file operations using the xp_cmdshell procedure, how to copy files, delete, rename, move, create directories, etc.
This post is a complement to other file operations using other solutions:
- File Operations Using OLE Automation in SQL Server
- SQL Server - How to list, read, write, copy, delete and move files with CLR (C #)
To enable xp_cmdshell, simply run the command below:
1 2 3 4 5 | sp_configure 'advanced options', 1 RECONFIGURE sp_configure 'xp_cmdshell', 1 RECONFIGURE |
stpArchive_Exists stpArchive_Listar stpArchive_Ler stpWrite_File stpApaga_Archive stpCopia_File stpMove_File stpCria_Directory stpApaga_Directory
As I mentioned in some posts here on the blog, I'm not a fan of enabling and using xp_cmdshell, since it gives "powers" to any sysadmin user of the instance to execute any command that the user who starts the SQL Server service has privilege , from a dir command, to delete, format, etc.
For this reason, I prefer to use CLR, where you program the routines using the C # programming language and the .NET Framework and publish the routines in the instance. Both sysadmin and normal users are only able to use the methods implemented in the CLR library, not allowing unscheduled malicious code to run, but I understand that not every DBA wants to do the work of creating C # libraries and ends up choosing to use a simpler approach using xp_cmdshell and DOS and / or PowerShell commands.
That's it folks!
I hope you enjoyed this post and see you next time.
I tried to move the file, but gave "Access denied". What profile is missing?
Thank you helped me a lot