Skip to content

SQL Server 2019: Copy files through SQL Server

Three new interesting extended stored procedures comes with SQL Server 2019.
I was very interested to discover these new store procedures:

  • Sys.xp_copy_file is to copy a specific files from a folder to another
    • Syntax: exec master.sys.xp_copy_file ‘source folder+file’, ‘destination folder+file’
    • Example:
      exec master.sys.xp_copy_file 'C:\Temp\Sources\File1.txt', 'C:\Temp\Destinations\File1.txt'

Before the command:

After the command:


As you can see in my test, you will have these 2 information where indicate the sucess of the query:
Commands completed successfully.
Time: 0.315s

  • Sys.xp_copy_files is to copy multiple files though the wildcard Character from a folder to another
    • Syntax: exec master.sys.xp_copy_files ‘source folder+files’, ‘destination folder’
    • Example:
      exec master.sys.xp_copy_files 'C:\Temp\Sources\File*.txt', 'c:\Temp\Destinations\'

Before the command:

After the command:

  • Sys.xp_delete_files is to delete a specific file or multiple files though the wildcard Character in a folder
      • Syntax: exec master.sys.xp_delete_files ‘source folder+files’
      • Example:
        exec master.sys.xp_delete_files 'c:\Temp\Destinations\File*.txt'

    Before the command:

  • After the command:

I go a little forward to see how it’s reacted if the file already exists or when we update the source
I copy the first time the file ‘c:\ Temp\Sources\file1.txt’ to ‘c:\ Temp\Destinations\file1.txt’
I have one line “first Line”

I had to the source a second Line “second line”
I copy again the file and result…

As you can see, the second line is in the destination.
Without warning or question, the file is overwriting.

The funny part is when you create a file manually (file6.txt) and want to delete it through SQL Server, it’s not possible.

Only the file, where SQL Server account is the owner, will be deleted…

Before SQL Server 2019, to do these operations, we use xp_cmdshell. Now with this new version, we can do easily copy operation or delete operations for files in a folder.
It’s very good and useful! A customer already asks me to implement it…
See you!

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Stéphane Haby

Stéphane Haby

Stéphane Haby has more than 20 years of experience in Microsoft solutions, specializing in the installation, migration, best practices, and performance analysis of Microsoft SQL Server, as well as Microsoft Business Intelligence solutions (SharePoint, Office) and .NET development for the banking sector. A former Microsoft Most Valuable Professional (MVP) and early adopter of Microsoft Team System in France, he has authored several technical articles. He holds Microsoft Certified Solutions Associate (MCSA) and Expert (MCSE) for SQL Server, MCTS, MCITP for SQL Server 2008, and ITIL V3 Foundation certifications. His industry experience covers chemicals and pharma, banking, and financial services.View Author posts

182 posts since April 2011

Most read in Database

The Database posts our readers open most: the all-time favourites, and what is trending right now.

1 thought on “SQL Server 2019: Copy files through SQL Server”

  1. How can I use this command with variable file name? I have tried many variations of the following code.

    DECLARE @copyfile VARCHAR(100) = ‘GeocodeResults-1_Processed.csv’
    PRINT @copyfile

    DECLARE @InputDir VARCHAR(100) = ‘E:\SQL_Databases\CCSR\GeoCoordinates\Output\’ + @copyfile
    PRINT @InputDir

    DECLARE @OutputDir VARCHAR(100) = ‘E:\SQL_Databases\CCSR\GeoCoordinates\Processing\’ + @copyfile
    PRINT @OutputDir

    DECLARE @CommandLine VARCHAR(255) = ” + @InputDir + ” + ‘, ‘ + ” + @OutputDir + ”;

    PRINT @CommandLine;

    EXEC master.sys.xp_copy_file @CommandLine;

    I get the following results, I cannot seem to be able to surround the Input file name and Output file name with single quotes.

    GeocodeResults-1_Processed.csv
    E:\SQL_Databases\CCSR\GeoCoordinates\Output\GeocodeResults-1_Processed.csv
    E:\SQL_Databases\CCSR\GeoCoordinates\Processing\GeocodeResults-1_Processed.csv
    E:\SQL_Databases\CCSR\GeoCoordinates\Output\GeocodeResults-1_Processed.csv, E:\SQL_Databases\CCSR\GeoCoordinates\Processing\GeocodeResults-1_Processed.csv
    Msg 22052, Level 16, State 1, Line 23
    Error executing extended stored procedure: Invalid Parameter

    Completion time: 2024-03-25T05:18:53.6360085-06:00

Leave a Comment

Your email address will not be published. Required fields are marked *