SQL Server 2008:复制文件并重命名

Nem*_*emo 7 t-sql sql-server sql-server-2008

我在目录中有一个文件 \\myServer\Admin\temp\testtemp.txt

我需要写一个TSQL

  1. 搜索testtemp.txt文件.
  2. 如果存在,请创建它的副本并将其重命名为 Copytesttemp.txt
  3. 如果testtemp.txt已经在上面这样的目录中

    \\abcd\Admin\temp\Copytesttemp.txt
    
    Run Code Online (Sandbox Code Playgroud)
  4. 然后删除它并重新创建 Copytesttemp.txt

我该如何实现?谢谢.

Pau*_*aul 17

您可以使用xp_cmdshell运行您喜欢的任何DOS命令,例如

declare @cmdstring varchar(1000)

set @cmdstring = 'copy \\myServer\Admin\temp\testtemp.txt \\myServer\Admin\temp\Copytesttemp.txt'
exec master..xp_cmdshell @cmdstring 
Run Code Online (Sandbox Code Playgroud)

只需确保在您的安装上启用了xp_cmdshell.