天极Yesky
  • 笔记本电脑
    笔记本
  • 台式电脑
    台式机
  • 手机
    手机
  • 电脑硬件DIY
    DIY硬件
  • CPU
    主板
    音箱
  • 硬盘
    显卡
    键鼠
  • 内存光驱
    显示器
    机箱电源

  • 数码相机DC
    数码相机
  • MP3播放器
    MP3/MP4
  • 数码摄像机DV
    摄像机
  • 电脑外设
    外设
  • 网络
    网络
  • 服务器
    服务器
  • 数字家庭
    数字家庭
  • 群乐
    群乐
  • 产品报价 行情 经销商 渠道 评测 | 软件 设计 网页 开发 安全 论坛 E时代 游戏 图片 壁纸 下载 网摘 博客 索尼专区 Vista 科技奥运
    天极网
    ZIP files with a stored procedure
    作者: Matt Harper
    出处:
    责任编辑:
    [ 2004-06-17 19:34 ]


    Here is a simple SQL Server stored procedure that zips files from a source and destination directory that you pass when calling the procedure. It uses the DOS-based wzzip.exe to zip files specified into a zip file named the current date and time, e.g., 200303030809.zip. You must have the wzzip.exe file in the path you supply for the @ZIPEXE parameter. You could optionally remove this parameter and hard code the path to the wzzip.exe or add the wzzip.exe path to an environment variable.

    ALTER PROCEDURE dbo.SP_ZIPFAXFILES (@ZIPEXE VARCHAR(8000),@SOURCE VARCHAR(8000), @DEST VARCHAR(8000))
    AS
    DECLARE @WINZIP VARCHAR(8000),
            @ZipName VARCHAR(8000),
            @DateConvert SMALLDATETIME,
            @Result INT
    SET @DateConvert = GETDATE() --Declare the current date time so that
                                 --conversions happen at the same date time
    SET @ZipName = CONVERT(VARCHAR(10), @DateConvert, 112) +  --Add the date to the file name 
        SUBSTRING(CONVERT(VARCHAR(10), @DateConvert, 108), 1,2) + --add the hour to the file name
        SUBSTRING(Convert(varchar(10), @DateConvert, 108), 4,2) + --add the minute to the file name
        + '.ZIP'
    SET @Dest = @ZIPEXE + ' ' + @DEST + @ZipName 
    SET @WINZIP = @Dest + ' ' + @Source
    EXEC @Result = master.dbo.XP_CMDSHELL @Winzip, no_output
    
    By placing this stored procedure in the Master database, you can access this from any of your databases. The @Result variable can be used to verify that files were zipped sucessfully and then delete the original files. In one database, I actually have this procedure use the XP_SENDMAIL procedure to send an email notifying me if the process fails.

    For More Information

    • Feedback: E-mail the editor with your thoughts about this tip.
    • More tips: Hundreds of free SQL Server tips and scripts.
    • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
    • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
    • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
    • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.
    笔名:
    请您注意:

     遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

     天极网拥有管理笔名和留言的一切权利。
    相关内容