本文介绍了如何通过MS SQL Server删除目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下查询从Windows内部目录中删除文件
I am trying to delete a file from a directory inside windows using the following query,
exec xp_cmdshell 'del "C:\root\sfd_devtracker\'+@deletefile + '"';
当我执行此命令时,出现以下错误,
When i execute this command it gives the following error,
Incorrect syntax near '+'.
在@deletefile
变量中,我具有必须删除的文件名.我在这里做错了什么?
In @deletefile
variable i have the filename which i have to delete. What have i done wrong here?
推荐答案
xp_cmdshell 要求将文字字符串作为参数传递.您不能即时构造一个值.
xp_cmdshell requires that a literal string be passed as parameter. You cannot construct a value on the fly.
尝试一下:
DECLARE @cmd NVARCHAR(MAX) =
'xp_cmdshell ''del "C:\root\sfd_devtracker\' + @deletefile + '"''';
EXEC (@cmd)
请考虑必须启用xp_cmdshell
,例如在这种方式.
Consider that xp_cmdshell
must be enabled, for instance in this way.
这篇关于如何通过MS SQL Server删除目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!