Use SQL Server to determine if a file exists and then delete it (detailed) _mssql

Source: Internet
Author: User

In SQL Server, you can use the system internal stored procedure xp_fileexist to determine whether a file exists, and then use xp_cmdshell to delete the file if it exists. Xp_fileexist In addition to being able to determine whether a file exists, you can also determine whether a folder exists, and here is an example of using these two.

Deleting a file stored procedure
ALTER proc [dbo]. [Delfile_p]
(@path nvarchar (200))
As
DECLARE @result int
exec master.dbo.xp_fileexist @path, @result out--paths can have spaces
If @result = 1--1 exists This file, 0 does not exist
Begin
--If the path has a space, you must replace the space character before executing the Cmdshell, enclose in double quotes
Set @path = ' del ' + replace (@path, ', ' "')
EXEC Master.dbo.xp_cmdshell @path
End
Calling stored procedures exec MIS.dbo.delFile_P ' f:/internet Explorer 6 green version/install.log '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.