Home Forums SQL Server 2008 SQL Server 2008 - General SQL Script To Copy text file from one location to other and Delete the File after Copying tO some log folder RE: SQL Script To Copy text file from one location to other and Delete the File after Copying tO some log folder

  • Rakesh.Chaudhary (7/24/2012)


    I got the solution from one of my colleagues

    Please find the solution below

    DECLARE @from VARCHAR(200)

    DECLARE @to VARCHAR(200)

    DECLARE@to_move VARCHAR(200)

    DECLARE @sql VARCHAR(200)

    DECLARE @sql_mov VARCHAR(200)

    DECLARE @sql_del VARCHAR(200)

    SELECT @to = '\\X\20120724.txt',

    @from = '\\Y\20120724.txt',

    @to_move = '\\Z\20120724.txt'

    SET @sql = 'copy ' + @from + ' ' + @to

    SET@sql_mov = 'copy ' + @from + ' ' + @to_move

    SET@sql_del = 'del ' + @from

    EXECUTE master..xp_cmdshell @sql

    EXECUTE master..xp_cmdshell @sql_mov

    EXECUTE master..xp_cmdshell @sql_del

    If you're going to use xp_CmdShell for this, there is a MOVE command in DOS and in ROBOCOPY.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)