CREATE PROC usp_Copy_Unzip_DirFiles @FTPPath NVARCHAR(4000)= '', -- ftp 文件夹路径 @WorkPath NVARCHAR(4000) = '', -- work 文件夹路径 @FileName NVARCHAR(4000) = '', -- 文件名 @sqlCmd NVARCHAR(4000) = '', -- 要执行的 cmd 命令 @RegRule nvarchar(4000) = 'Regex*.zip', -- 定义要拷贝的文件名的规则 @7zipPath NVARCHAR(4000) = 'IPCC$"Program Files"7-Zip7z.exe', -- 原理即调用 7z 解压软件解压,所以 IPC 对应到相应的网络磁盘路径 @IsZipped BIT = 1 -- 输入参数,确认拷贝的 是不是 压缩文件,只有压缩文件,下面的脚本解压部分才需要执行 AS SET @sqlCmd = 'dir /b '+@FTPPath+@RegRule IF OBJECT_ID('tempdb..#dirlisttab') IS NOT NULL DROP TABLE tempdb..#dirlisttab CREATE TABLE tempdb..#dirlisttab ( FILENAME Nvarchar(128) ) INSERT INTO #dirlisttab EXEC xp_cmdshell @sqlCmd --SELECT * FROM #dirlisttab DECLARE file_cur CURSOR LOCAL STATIC FORWARD_ONLY FOR SELECT [FILENAME] FROM #dirlisttab OPEN file_cur WHILE 1=1 BEGIN fetch next from file_cur into @FileName SET @sqlCmd = 'copy '+@FTPPath+@FileName+' '+@WorkPath+@FileName print @sqlcmd EXEC xp_cmdshell @sqlcmd IF(@IsZipped = 1) BEGIN SET @sqlCmd = @7zipPath+' x '+@WorkPath+@FileName+' -y -aos -o'+@WorkPath print @sqlcmd EXEC xp_cmdshell @sqlcmd END if @@fetch_status <> 0 break; END CLOSE file_cur DEALLOCATE file_cur SET @sqlCmd = ' dir /b '+@WorkPath+'*.txt > '+@WorkPath+'FileName|del '+@WorkPath+'*.zip /Q' print @sqlcmd EXEC xp_cmdshell @sqlcmd DROP TABLE tempdb..#dirlisttab GO