• [转]COPY OR MOVE FILES AND FOLDERS USING OLE AUTOMATION


    本文转自:http://sqlindia.com/copy-move-files-folders-using-ole-automation-sql-server/

    I love playing around with automation stuff. In a recent automation task, I was to copy or move files and/or folders from one location to another in SQL Server without using SQLCMD.
    If you are novice to OLE automation in SQL server then I would recommend you to read my previous articles on OLE automation first.
    
    --The below configuration must be enabled 
    
    /* 
    sp_configure 'show advanced options', 1; 
    GO 
    RECONFIGURE with override; 
    GO 
    
    sp_configure 'Ole Automation Procedures', 1; 
    GO 
    RECONFIGURE with override; 
    GO 
    */ 
    
    
    DECLARE @source varchar(100) = 'E:othersLab_SQLIndiasrcTextFile.txt' 
    , @destination varchar(100) = 'E:othersLab_SQLIndiadest' 
    , @operation tinyint = 1 -- 1=CopyFile, 2=CopyFolder, 3=MoveFile, 4=MoveFolder,   
    
    
    DECLARE @fso int 
    , @init int 
    , @errFso int 
    , @return varchar(100) 
    SET @destination = @destination + '' 
    
    EXEC @init = sp_OACreate 'Scripting.FileSystemObject', @fso OUTPUT 
    IF @init = 0 SELECT @errFso = @fso 
    
    IF @init = 0 AND @operation = 1 
    
    
    BEGIN 
    
    IF @init = 0 SELECT @errFso = @fso 
    
    EXEC @init = sp_OAMethod    @fso, 
                    'CopyFile', 
    NULL, @source, @destination SET @return = 'FILE SUCCESSFULLY COPIED' END ELSE IF @init = 0 AND @operation = 2 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'CopyFolder', NULL, @source, @destination SET @return = 'FOLDER SUCCESSFULLY COPIED' END ELSE IF @init = 0 AND @operation = 3 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'MoveFile', NULL, @source, @destination SET @return = 'FILE SUCCESSFULLY MOVED' END ELSE IF @init = 0 AND @operation = 4 BEGIN IF @init = 0 SELECT @errFso = @fso EXEC @init = sp_OAMethod @fso, 'MoveFolder', NULL, @source, @destination SET @return = 'FOLDER SUCCESSFULLY MOVED' END IF @init <> 0 BEGIN DECLARE @Description varchar (255), @src varchar (255), @Helpfile varchar (255), @HelpID int EXECUTE sp_OAGetErrorInfo @errFso, @src OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT PRINT COALESCE(@src + @Description + @Helpfile + CAST(@HelpID as varchar(20)), 'Error Occurred') END ELSE PRINT @return EXEC sp_OADestroy @fso
  • 相关阅读:
    java面试-synchronized底层实现机制
    java面试-内存分配与回收策略
    java面试-对象的创建、内存布局、访问定位
    推荐算法-余弦相似度
    推荐算法-欧几里得距离
    MySQL总结
    MySQL事务
    MySQL-一条sql语句的执行顺序
    mysql 表格操作指令大全(Show、desc、create、insert into、delete、select、drop、update、alter)
    mysql 创建数据数据库 (避免新建的库名已经存在、设置编码)
  • 原文地址:https://www.cnblogs.com/freeliver54/p/8063885.html
Copyright © 2020-2023  润新知