zoukankan      html  css  js  c++  java
  • 备份删除



    CREATE TABLE #files
    (
    id INT IDENTITY(1, 1) ,
    name VARCHAR(500)
    )
    DECLARE @dir VARCHAR(600)= 'E:\DataBaseBackup\'

    DECLARE @dir1 VARCHAR(600)= 'dir ' + @dir

    DECLARE @day INT= 8
    --大于等于8天的备份

    INSERT #files ( name )
    EXEC xp_cmdshell @dir1;
    WITH cte
    AS ( SELECT name = @dir + RTRIM(LTRIM(REVERSE(LEFT(REVERSE(name),
    CHARINDEX(' ',
    REVERSE(name))))))
    FROM #files
    WHERE id > 5
    AND name NOT LIKE '%DIR%'
    AND name NOT LIKE '%字节%'
    )
    SELECT name
    INTO #t1
    FROM ( SELECT CASE WHEN id >= @day THEN '11'
    ELSE NULL
    END id, name
    FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY name DESC ) id,
    *
    FROM cte S0
    WHERE S0.name LIKE '%SGPZ%'
    UNION ALL
    SELECT ROW_NUMBER() OVER ( ORDER BY name DESC ) id,
    *
    FROM cte S0
    WHERE S0.name LIKE '%SCT%'
    ) s1
    ) s2
    WHERE s2.id IS NOT NULL



    DECLARE @ACTION1 VARCHAR(MAX)

    DECLARE pcurr CURSOR
    FOR
    SELECT name
    FROM #t1

    OPEN pcurr
    FETCH NEXT FROM pcurr INTO @ACTION1
    WHILE ( @@FETCH_STATUS = 0 )
    BEGIN

    DECLARE @cmd VARCHAR(4000)= ''



    SET @cmd = 'COPY ' + @ACTION1
    + CASE WHEN @ACTION1 LIKE '%SGPZ%'
    THEN ' ' + REPLACE(@ACTION1, 'E:\DataBaseBackup\',
    'd:\DataBaseBackup\SGPZ\')
    ELSE ' ' + REPLACE(@ACTION1, 'E:\DataBaseBackup\',
    'd:\DataBaseBackup\SCT\')
    END
    PRINT @cmd

    EXEC xp_cmdshell @cmd --拷贝

    SET @cmd = 'del ' + @ACTION1

    PRINT @cmd

    EXEC xp_cmdshell @cmd --删除

    ----------------------------
    FETCH NEXT FROM pcurr INTO @ACTION1
    END
    CLOSE pcurr
    DEALLOCATE pcurr




    DROP TABLE #files
    DROP TABLE #t1

  • 相关阅读:
    Docker 安装各种环境
    N级树形菜单封装
    一个周期算出所有高电平的个数
    DDR3新版(3):DDR3自动读写控制器
    DDR3_新版(2):IP核再封装
    数电(6):时序逻辑电路
    Vue表单那些事
    liunx环境修改minio默认端口和后台启动
    liunx 后台启动mongodb服务
    liunx安装和部署nacos配置中心
  • 原文地址:https://www.cnblogs.com/qanholas/p/2747766.html
Copyright © 2011-2022 走看看