Is it even possible to access a file in a local drive, copy it, and place the copy in another location? If it is, can anyone show me how?
I am trying to create a job via the SQL Server Agent that copies a file and store it in another location.
Is it even possible to access a file in a local drive, copy it, and place the copy in another location? If it is, can anyone show me how?
I am trying to create a job via the SQL Server Agent that copies a file and store it in another location.
Yes, it is possible by using the system procedure xp_cmdshell.
You can use any DOS commands using this procedure!
As an example you can use below command:
/* build copy command */
SET @Cmd = 'COPY "C:\temp\1.bmp" "D:\1.bmp"';
/* execute copy command */
EXEC master.dbo.xp_cmdshell @Cmd;
Note: Because of security risk, this procedure is disabled by default since a few version ago. You need to enable it, but note that this procedure is a security hole if your network is not secure.
Also note that this is just a utility procedure, it might not be good idea to use it in applications except for very special scenarios. If you are going to use it in your application, you may need to rethink about your design.
To enable the xp_cmdshell use below commands:
-- To allow advanced options to be changed.
EXECUTE sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXECUTE sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO